How To: Provide Save File Dialog for an Image Request in ASP.Net
It is the default behavior of the web browsers, when you click on the image link, to open the image and display it over there. There are instances when you might want your users to remain on the page and simply download and save the image after clicking on the image link.
This functionality can be achieved in ASP.Net. Here I am going to show how to:
Create a blank ASP.Net Web Application. Now you have to create a folder to store the image to be downloaded and add and image to it.
Your solution should look similar to this:
Open the Default.aspx and create a HyperLink to the image like this:
<asp:HyperLink ID="hlDownloadImage" runat="server" NavigateUrl="~/Images/image001.jpg?Action=Download">Download Image</asp:HyperLink>
Note that the image URL is having additional parameter "Action" with value "Download". We will see the utilization of the same in a moment.
now go and add a new file of Type ASP.Net Module ImageDownload.cs to the solution and modify the code as below:
public class ImageDownload : IHttpModule
{
#region IHttpModule Members
public void Dispose() { }
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
#endregion
public void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpRequest req = app.Request;
HttpResponse res = app.Response;
if (req.Params["Action"] != null && req.Params["Action"].ToString() == "Download")
{
string path = req.AppRelativeCurrentExecutionFilePath;
res.ContentType = "image/jpeg";
res.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(path));
res.TransmitFile(app.Server.MapPath(path));
res.End();
}
}
}
Modify the web.config file to include following in <httpModules> section:
<add name="ImageDownload" type="WebUploadManager.ImageDownload"/>
You are done. Run the application and click on the Link for the image and you will see the Run/Save dialog. Try removing the Action parameter from the NavigationURL of the hyperlink; image will be displayed on screen as the default behavior of the browser.

January 28th, 2011 - 08:33
Excellent blog here! Also your web site loads up very fast! What web host are you using? Can I get your affiliate link to your host? I wish my website loaded up as quickly as yours lol
February 2nd, 2011 - 08:44
Yes I know … What you mean to say … It is actually very slow. i am looking for host where I can host multiple sites/domains for asp.net and php both.
July 8th, 2011 - 17:18
Didnt know the forum rules alwloed such brilliant posts.
July 9th, 2011 - 23:36
It’s much easier to unedsrtand when you put it that way!
July 8th, 2011 - 12:08
Created the grteaest articles, you have.
July 9th, 2011 - 20:04
Superior thinking dmeontsrtaed above. Thanks!
February 26th, 2011 - 17:49
Thanks for the entry, I even learned a lot from it. Really quality content on this blog. Always looking forward to new entry.
March 1st, 2011 - 08:47
Very interesting and cool.. Very nice sharing. Thanks for such a nice post. I really appreciate it.
May 9th, 2011 - 05:38
Hi
I am trying to use your code, i liked it, but i get error in
If i remove “WebUploadManager” from type it works. but then the image opens without dialog box in browser
Please help what i am missing
Thanks
May 9th, 2011 - 12:43
Can you please create a small working sample demo?
July 8th, 2011 - 23:50
Hats off to wohveer wrote this up and posted it.
July 9th, 2011 - 19:42
Boy that rellay helps me the heck out.
May 9th, 2011 - 13:36
Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in
machine.config.comments usually located in
\Windows\Microsoft.Net\Framework\v2.x\Config
–>
This is my web.config? is there something wrong
<!–
The section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
–>
<!–
The section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.
–>
July 6th, 2011 - 15:35
Your article perfectly shows what I ndeeed to know, thanks!
July 8th, 2011 - 19:03
This has made my day. I wish all psotings were this good.
May 9th, 2011 - 13:36
Sorry i tried to post web.config but it didnt allowed
May 16th, 2011 - 19:30
I will try to creat the sample app and upload it here.
July 8th, 2011 - 23:23
Smack-dab what I was lokonig forty!
July 9th, 2011 - 18:52
You’re the greaestt! JMHO
May 17th, 2011 - 00:06
here you go …
- Download this archive: http://codes.anantanandgupta.net/ImageDownload.zip
- extract and put the dll in your bin folder.
- put the following line in your web.config in httpModules section:
- browse any image in your site along with the ?Action=Download attached to the URL as parameter.
May 17th, 2011 - 00:11
I think the XML tags are not allowed here, here you go with the code:
add name=”ImageDownload” type=”MindzGroup.Web.Modules.ImageDownload, MindzGroup.Web.Modules”
July 6th, 2011 - 15:12
Waklnig in the presence of giants here. Cool thinking all around!
July 6th, 2011 - 15:36
Wow! Great tnhiking! JK
July 8th, 2011 - 01:00
That addresses several of my concerns autcally.
July 8th, 2011 - 19:12
This has made my day. I wish all positgns were this good.
July 9th, 2011 - 18:26
At last, someone comes up with the “right” asenwr!
May 12th, 2011 - 18:54
Heya just wanted to give you a quick heads up and let you know a few of the images aren’t loading properly. I’m not sure why but I think its a linking issue. I’ve tried it in two different web browsers and both show the same outcome.
May 16th, 2011 - 19:23
I am not able locate any missing image. Can you please let me know which post you are talking about … Thanks!
July 6th, 2011 - 13:29
Frnalky I think that’s absolutely good stuff.
July 7th, 2011 - 20:58
Thank you so much for this arcitle, it saved me time!
July 8th, 2011 - 00:35
There is a critical shortage of inofrmative articles like this.
July 7th, 2011 - 20:32
Cool! That’s a ceevlr way of looking at it!
July 2nd, 2011 - 04:27
This is a smart blog. I mean it. You have so much knowledge about this issue, and so much passion. You also know how to make people rally behind it, obviously from the responses. Youve got a design here thats not too flashy, but makes a statement as big as what youre saying. Great job, indeed.
July 2nd, 2011 - 17:27
I would like to thank you for the efforts you have made writing this article. You have been enlightening for me. I’ve passed this on to a friend of mine.
July 6th, 2011 - 13:41
Heck of a job there, it asbluoetly helps me out.
July 8th, 2011 - 00:31
You keep it up now, undesrantd? Really good to know.
July 8th, 2011 - 05:15
This is exactly what I was looking for. Thanks for wiritng!
July 8th, 2011 - 07:31
With the bases laoded you struck us out with that answer!