views:

57

answers:

2

Hi All,

I am using asp.net 3.5 and C#.

I have a image which I want user can download. Like, there would be a download button or link. When user click on this link he will be prompted with a pop up to save that image to his desktop.

I have tried with

<a href ="path" > </a>

but it is opening the image in other page, I want user to be prompted to either save or view the image,

please help

Thanks in advance

+2  A: 

You need to write an IHttpHandler that serves the image along with a Content-Disposition header.

For example:

Response.AppendHeader("Content-Disposition", "attachment; filename=\"MyImage.png\"");
Response.TransmitFile(path);

You would probably pass the image name on the query-string.
If so, make sure it doesn't contain / or \, or attackers will be able to read arbitrary files.

SLaks
You could always limit the paths that you allow to be used or, at least, validate that they are located in the image directory. It is important to make sure that you don't have any shenanigans going on and restricting it to just a name is probably the easiest way to do this.
tvanfosson
A: 

You need to have another page or, better yet, an HttpHandler, that takes the image path as part of the query string or as a post parameter that will send the response with Content-Disposition set to attachment. Setting the content disposition this way will cause the browser to display the file download dialog. A slightly easier way, though it depends on the user doing something extra is simply to have the link open the image in a new page and let the user right-click on it and do a "Save As".

<a href="/path/to/handler?image=name-of-image-file">Download</a>

or

<a href="/path/to/image" target="_blank">
    Load Image in New Window then Use Save As</a>
tvanfosson
Thanks !!! This solved my issue.
Zerotoinfinite