Show photo on ASP

P

Phil H

I have an .NET ASP web application to show a photo depending on the person
selected. I used the url tag pointing to a file local to the server. All
works fine when running from the IDE. Problem: when I deploy it, I got a
permission error when creating the file. Question: where do I specify the
permission.
I am pretty new with web app, so any suggestion to alternate method is
welcome also.

Thanks.
 
N

Nicholas Paldino [.NET/C# MVP]

Phil,

This is most likely because on the server, ASP.NET is running under the
ASPNET local account, which doesn't have rights to the directory that you
specified. You have to either change the account that your application runs
under in the web.config file, or grant the ASPNET account permissions to
that directory.
 
M

Marc Gravell

I got a permission error when creating the file

Can you clarify; *showing* a photo sounds like a read operation, not a
write; are you writing the file into the local web? Generally, a
better option would be to simply stream it from source, perhaps using
a bespoke handler. You could also use virtual paths to bring the image
into the web itself.

Marc
 
P

Phil Hunt

I am reading the image from a database and writing into a file, No problem
there. I just cannot get the ImageURL to point to the right folder.

How do you stream the file into the web itself ? Where can I find out how ?

Thanks
 
M

Marc Gravell

I am reading the image from a database and writing into a file, No problem
there. I just cannot get the ImageURL to point to the right folder.

How do you stream the file into the web itself ? Where can I find out how ?

If I understand correctly, you simply don't need to write a file. What
I would do is in my page html (from aspx) page, refer to an image
(that doesn't exist) [see later], and then use a handler (e.g. ashx)
to serve the image. A handler is broadly just a fancy name for a class
that responds to requests, often much simpler than a full-blown aspx.
All it needs to do is:
* set the content-type so that the browser knows if it an image (and
what type)
* write the raw image to the response-stream

The latter is identical to working with a FileStream. The "handler"
step can be done in 2 ways; you can add a handler via the IDE and get
an .ashx file (with a query-string such as my.ashx?id=12345);
alternatively you can do the same without the actual ashx file, by
editing the httpHandlers section of web.config (you can then make the
URL look tidier, such as 12345.gif). Either way, the first thing your
handler has to do is to parse the URL to find which image to serve,
and fetch the binary.

A similar example: http://weblogs.asp.net/cazzu/archive/2003/08/27/25568.aspx
A good overview of handlers: http://msdn2.microsoft.com/en-us/library/ms972953.aspx

Marc
 
P

Phil H

Thanks Marc.
It is a little over my head now. Let me see if if I can use the example and
learn somthing. Thanks for taking the time.

Phil
Marc Gravell said:
I am reading the image from a database and writing into a file, No
problem
there. I just cannot get the ImageURL to point to the right folder.

How do you stream the file into the web itself ? Where can I find out how
?

If I understand correctly, you simply don't need to write a file. What
I would do is in my page html (from aspx) page, refer to an image
(that doesn't exist) [see later], and then use a handler (e.g. ashx)
to serve the image. A handler is broadly just a fancy name for a class
that responds to requests, often much simpler than a full-blown aspx.
All it needs to do is:
* set the content-type so that the browser knows if it an image (and
what type)
* write the raw image to the response-stream

The latter is identical to working with a FileStream. The "handler"
step can be done in 2 ways; you can add a handler via the IDE and get
an .ashx file (with a query-string such as my.ashx?id=12345);
alternatively you can do the same without the actual ashx file, by
editing the httpHandlers section of web.config (you can then make the
URL look tidier, such as 12345.gif). Either way, the first thing your
handler has to do is to parse the URL to find which image to serve,
and fetch the binary.

A similar example:
http://weblogs.asp.net/cazzu/archive/2003/08/27/25568.aspx
A good overview of handlers:
http://msdn2.microsoft.com/en-us/library/ms972953.aspx

Marc
 
M

Marc Gravell

Minor aside; the "read" steps would be tidier as follows:

while ((size = r.GetBytes(1, idx, buffer, 0, ChunkSize)) > 0)
{
context.Response.BinaryWrite(buffer, 0, size);
idx += size;
}


(and remove the "last bytes" stuff)
This deals with a few edge-conditions more gracefully...
 

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

Top