Solved

Read & Output Image File to Browser/Control?

Posted on 2004-08-03
10
299 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
0
 
LVL 7

Expert Comment

by:mdamico
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now