Reputation: 473
I have a problem downloading an image from a web browser. I want to download the image directly, but instead it's opened in the browser. I'm using asp.net.
My HTML code:
<a href ="http://example.com/file/image.jpg" target="_blank" /> Download </a>
Upvotes: 2
Views: 2936
Reputation: 30922
What you need to do here is to modify the HTTP headers to in a way that requests the browser to show the "File Dialog" box for your image instead of simply displaying it on screen.
To do this you need to modify the Content-Disposition
header and set it to attachment
. To do this in ASP.NET you can do the following:
Response.Clear()
Response.AppendHeader("Content-Disposition", "attachment; filename=somefilename")
but make sure you do this before you respond with the file.
You might also want to change the following:
Response.ContentType = "image/jpeg"
This will allow the browser to regonise and display the image icon in the File Dialog box. To finally send the file you would then call:
Response.TransmitFile(Server.MapPath("/myimage.jpg"));
Response.End();
However please realise all you are doing here is modifying what the server requests of the browser - it is not bound in anyway carry it out.
Upvotes: 4
Reputation: 5622
Try the following method (assuming you're using c#)
Response.ContentType = "image/jpg";
Response.AppendHeader("Content-Disposition", "attachment; filename=myimage.jpg");
Response.TransmitFile(Server.MapPath("/images/image.jpg"));
Response.End();
Upvotes: 0