Solved

Read & Output Image File to Browser/Control?

Posted on 2004-08-03
10
305 Views
Last Modified: 2012-06-21
How can I do the following:

Read an image file from a private directory not accessible to the webserver, and then output it to an image control of some sort on an ASP.Net page.

Obviously I can read the file, output it to a temporary file, and then serve it, but I would prefer to avoid having to clean up afterwards.

Suggestions?
0
Comment
Question by:Jimby_Aus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11708809
if its not accessible to the websever how can you read it?  This illustrates how to read directly into a db, should only require slight modification to display:

<form runat="server" enctype="multipart/form-data" ID="Form2">
  <P>
    <input type="file" id="file1" runat="server" NAME="file1">
  </P>
  <P>
    <asp:Button id="btn1" runat="server" text="Upload" onclick="upload" />
  </P>
</form>

--------------------------------------------------------------------------------------

Imports System.Data
Imports System.Data.SqlClient

Public Sub Upload(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim b(file1.PostedFile.InputStream.Length - 1) As Byte

        file1.PostedFile.InputStream.Read(b, 0, file1.PostedFile.InputStream.Length)

        Dim con As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionStringSQL"))

        Dim sql As String = "INSERT INTO MY_TABLE(MyID, DATABLOB) VALUES(1,@BlobData) "
        Dim cmd As New SqlCommand(sql, con)

        Dim parmBlob As New SqlParameter("@BlobData", SqlDbType.Image, _
                     b.Length, ParameterDirection.Input, False, 0, _
                     0, Nothing, DataRowVersion.Current, b)
                     
        cmd.Parameters.Add(parmBlob)

        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()
End Sub

Regards,

Aeros
0
 
LVL 1

Author Comment

by:Jimby_Aus
ID: 11708966
Thanks, I can do the read side of this, but instead of inserting it into a field, I need to output it to the browser, preferably to a control.

When I said its not accessible to the webserver, I mean that the image file is outside (above) the application root web directory, and so I cannot reference the file using HTML.

Jim.
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11709006
Well thats what I thought you meant originally, so the bottom line is how can you expect IIS to display an image without having access to it?
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 17

Expert Comment

by:AerosSaga
ID: 11709036
0
 
LVL 7

Expert Comment

by:mdamico
ID: 11710818
Give the ASPNET account permission to the directories you want to access images from.  Your ASP.Net code will be able to access the images, but someone visiting your site will not be able to access them directly.

To output the images you can do something like (example shows creating an image on the fly, but you do the same with an existing image):

http://www.developerfusion.com/show/2569/

0
 

Expert Comment

by:burly411
ID: 11710897
Another solution would be to read the file into a byte array and then display it to the screen using the response.write() function.

This will of course write it to the top of you're webpage

So create a filestream variable and read in the data to a byte array:

dim fileData as byte()
dim fs as filestream = new filestream(pathname, filemode.open, fileaccess.read)

' Note be sure to set the lenght of the file
fs.Read(fileData, 0, fileData.Length)

'now display it to screen using: (and again make sure to determine the contenttype of the file)
' The contentype is a string
Response.ContentType = contentType
Response.OutputStream.Write(fileData, 0, fileData.Length);\

That will display the picture on your page
0
 
LVL 1

Accepted Solution

by:
v2000 earned 500 total points
ID: 11711000
Another solution similar to those above (I am happy to sacrifice points if it's too similar):

Make an ASPX (ReturnImage.aspx) file that accepts as input some identifier for the image via the QueryString (accessible via Request.QueryString).  Set the Response.ContentType to "image/jpeg" or "image/gif" or whatever the image file is.  Then open the file with a filestream and response.binarywrite it.

Then, in your original page, make an image object and set the src property to the new ASPX file, so that the HTML that results is:

<img src=http://yourserver/ReturnImage.aspx?ImageID=c:\somewhereElse\image.jpg>

Understand?  So your ReturnImage.aspx file will act like an image to the original web page, which merely has to pass some image identifier (I use the filename above, which is sort of insecure) as part of the "src" property of the image object.

Hope this helps.
0
 
LVL 24

Expert Comment

by:Justin_W
ID: 11711703
See this PAQ:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21038432.html

It describes how to create an ASPX page that can be used to dynmaically display (based on URL params) images from a DB or other sources.  (This is similar to v2000's comment above.)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question