Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Read & Output Image File to Browser/Control?

Posted on 2004-08-03
10
Medium Priority
?
310 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
9 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

783 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