Solved

Read & Output Image File to Browser/Control?

Posted on 2004-08-03
10
302 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
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 17

Expert Comment

by:AerosSaga
ID: 11709036
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11709053
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

808 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