Temporary user files under IIS - FAQ?

R

Rob Nicholson

What's the usual way of handling temporary user files under ASP.NET running
on IIS? On a normal Windows client program, you'd probably store the
documents in the %TEMP% folder. However, aren't all users running under the
same ASPNET account so they're sharing the same TEMP folder? Also, you
really want them within the web-site folder so that normal HTML <IMG> tags
can be used.

We're using a temporary file to hold a user's photograph. This photograph is
stored in a SQL database as an IMAGE column. When the record is opened, we
need to create the temporary file with the same file name so that if the
user right-clicks, the default file name is correct, i.e. the original.

At the moment, we've created a User folder in the web site and build a
unique path using the session ID like this:

c:\inetpub\wwwroot\ourwebsite\users\37873827387283\porky.jpg

Where 37873827387283 is the session ID. It works but doesn't feel quite
right :) We also have to manually grant ASPNET write access to the Users
folder.

Any better ideas?

Thanks, Rob.
 
J

Josh

I think your approach is wrong. You can write files directly to the
response stream ( and give them any name you want. read the file out of SQL
into an image ( bitmap) object and write that to the stream.
 
R

Rob Nicholson

I think your approach is wrong. You can write files directly to the
response stream ( and give them any name you want. read the file out of SQL
into an image ( bitmap) object and write that to the stream.

Never used response streams but understand that you're suggesting. This
isn't a full screen image - it's a page containing information about a
person with an image control on the page. If you were mocking it up, you'd
have something like:

<asp:Image
id="Image1"
style="Z-INDEX: 100; LEFT: 704px; POSITION: absolute; TOP: 152px"
runat="server"
ImageUrl="Porky.jpg"
BorderStyle="Solid"
BorderWidth="1px"
BorderColor="#7F9DB9" Height="234px"
Width="234px">
</asp:Image>

The advantage of using a <IMG> tag (which is I assume what asp:Image ends up
as) is that if the user right-clicks, then can save-as using the original
file name.

I can't see how you can repeat this functionality using a response stream
which I think is basically an "on-the-fly" HTTP response sent back to the
browser. It's not stored anywhere except within the browser to render the
current page. It can't be linked to via a IMG SRC tag.

Cheers, Rob.
 
J

Josh

I'm thinking on my feet here but I'm sure you can write your file into the
midle of the html.

If you call a function from the HTML e.g.


<HTML>
<BODY>
<TABLE><TR><TD>
<% =MyFunction() %>
</TD></TR></TABLE>
</BODY>
</HTML>


then in the code behind

public void MyFunction()
{
Response.Write("<IMG") ....
}
 
H

Hans Kesting

Rob said:
Never used response streams but understand that you're suggesting.
This isn't a full screen image - it's a page containing information
about a person with an image control on the page. If you were mocking
it up, you'd have something like:

<asp:Image
id="Image1"
style="Z-INDEX: 100; LEFT: 704px; POSITION: absolute; TOP: 152px"
runat="server"
ImageUrl="Porky.jpg"
BorderStyle="Solid"
BorderWidth="1px"
BorderColor="#7F9DB9" Height="234px"
Width="234px">
</asp:Image>

The advantage of using a <IMG> tag (which is I assume what asp:Image
ends up as) is that if the user right-clicks, then can save-as using
the original file name.

I can't see how you can repeat this functionality using a response
stream which I think is basically an "on-the-fly" HTTP response sent
back to the browser. It's not stored anywhere except within the
browser to render the current page. It can't be linked to via a IMG
SRC tag.

Cheers, Rob.

An html-page with one image gives two requests to the browser: html-file
and image. (so you can't send the image along with the page)

Write the img-tag with an "src" pointing to some "getImage.aspx" (with appropriate
arguments to get the correct image).
In that getImage.aspx you read the image from the database and write it
(with Response.BinaryWrite) to the output (by the way: make sure the "aspx"
part contains no html code!).
But just before you do that, add some http headers:
Response.AppendHeader("Content-Disposition", "inline; filename=myfile.jpg");
Response.ContentType = "image/jpg";

When the user wants to save the image, he doesn't get "getImage.aspx", but
"myfile.jpg" as default filename. (of course: change that name to what you want)

No need for temporary files!

One thing you could change: when you retrieve the data for the page (including
the image), store that in Session. Then getImage.aspx doesn't need parameters,
it can just read the stored image.


Hans Kesting
 
S

Steven Cheng[MSFT]

Thanks for all your informative inputs.

Hi Rob,

As other members as mentioned, the <img src=... /> infact cause an
additional request to server to request the image. Generally this is a
static image file on the server. However, when the images we want to
display is to be retrieved from database, using static images files will be
abit hard since it need IO operations in our web application. Also,
creating those temporary files will need constant cleanup task , otherwise
there'll cause duplicated unuse files in the temp folder.

So in addition to your current solution, I think we can consider using a
HttpHandler to dynamically retrieve image and writeout as Image response
stream so that our <img src=../> tag can reference it. For example, our
aspx page which display image will become something like:

....
<img src="imagehandler.axd?param1=xxx&param2=xxx" />

Here is a good tech article discussing building such an httphandler:

#Using ASP.NET HTTP Handlers to create a photo album
http://www.microsoft.com/belux/nl/msdn/community/columns/desmet/httphandler.
mspx

Hope also helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Hi Rob,

Any further progress on this? Have you chosen a certain means for exposing
your image streams from db? Anyway, if there are still anything we can
help, please feel free to let us know. Thanks,

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top