开发者

Pop up to save image in asp.net

开发者 https://www.devze.com 2023-01-01 17:31 出处:网络
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 t

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开发者_StackOverflow社区 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


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.


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消