469,946 Members | 1,987 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,946 developers. It's quick & easy.

Maintain an uploaded Image in a Session


I have a ASP.NET form with Web layout which I've achieved using panels. In
one of the tab I have a File control to upload Images. When I put a file in
the file control and move to another tab, I am loosing the file in the file
control. So, I am putting the file in a Session:

Session("PhotoFile") = filePhoto.PostedFile

When the user comes back to the tab with the File upload, an Image control
displays the Image from the session. To make this work, I have built another
aspx page with only code to retrieve the Image from the session. To the
Image control in the parent form, I 'm setting it's ImageURL property to
"Image.aspx" (The file which contains code to retrieve the Image from the

Dim lpostedFile As HttpPostedFile = Session("PhotoFile")

Dim lfilePhoto As HttpPostedFile = Session("PhotoFile")

Dim lintFileSize As Integer = lfilePhoto.ContentLength

Dim lsFileName As String = Right(lfilePhoto.FileName, _

Len(lfilePhoto.FileName) - lfilePhoto.FileName.LastIndexOf("\") - 1)

Dim lsContentType As String = lfilePhoto.ContentType

Dim lstreamImage As IO.Stream = lfilePhoto.InputStream

Dim lbytePhoto(lintFileSize) As Byte

lstreamImage.Read(lbytePhoto, 0, lintFileSize)

Response.ContentType = lsContentType


This works fine when I come back to the Image tab for the first time. The
Image control displays the uploaded Image. But If I refresh the page or go
to another tab and return back, the Image is not displayed. When I debug,
all the varibales display correct values which means there is no problem
with the Session.

I have worked around this by Saving the uploaded file to the server and
setting the Image's ImageURL property to the file. This works but I don't
want to do this as the site will be used globally and over the time this
will affect other resources like disk space. Can somebody please help me
with this.

Also, I've a report where I retrieve the Images from the database and
display them on the web page. As this report is too customized, I've to
build the page by dynamically generating HTML using the script. How can I
display each Image from the database on the page? Trying to save the Image
in the file system and then putting in to the HTML is going to be very very
expensive on the file system. Or generating the Image in a separate aspx
page and setting src of the Image to the aspx file will display the last
retrieved Image by Image.aspx for all the Images in the report. Please
provide me a solution for this too...

The last thing I can do is, as this site is an Intranet site and needs
authentication for the users, create a directory for the session in the
server and put all the Image files used by the user in to the directory.
When the session ends, delete the directory. But there should be a better
solution for this.

Thanks in advance.

Thank you.

Satish Appasani
#201, Wing - 1, Block - D
Cyber Gateway
Hyderabad - 500 081, India
Phone: +91(40)2311-1356 Ext-122
Mobile: +91(40)333-31032
E-mail: sa****@vertexcs.com
Nov 17 '05 #1
0 2002

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Mark | last post: by
5 posts views Thread by Brian Lowe | last post: by
5 posts views Thread by IkBenHet | last post: by
reply views Thread by IkBenHet | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.