Jimby_Aus
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?
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?
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.
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?
Your only option I think would be something like this:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dngenlib/html/msdn_manamemo.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dngenlib/html/msdn_manamemo.asp
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/
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.Writ e(fileData , 0, fileData.Length);\
That will display the picture on your page
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.Writ
That will display the picture on your page
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.)
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.)
<form runat="server" enctype="multipart/form-da
<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.InputSt
file1.PostedFile.InputStre
Dim con As New SqlConnection(Configuratio
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(parmBlo
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub
Regards,
Aeros