Link to home
Start Free TrialLog in
Avatar of Jimby_Aus
Jimby_AusFlag for Australia

asked on

Read & Output Image File to Browser/Control?

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?
Avatar of AerosSaga
AerosSaga

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
Avatar of Jimby_Aus

ASKER

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.
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?
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/

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
ASKER CERTIFIED SOLUTION
Avatar of v2000
v2000

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
See this PAQ:
https://www.experts-exchange.com/questions/21038432/picture-not-displaying-on-aspx-page.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.)