Skip to content Skip to sidebar Skip to footer

Open "save As" Dialog Box To Download Image

I've been checking all over the place for an answer to this one but no luck. I want a button or link that will open the 'Save As' dialog box. Simple? I know I can open the image in

Solution 1:

You can create a button that point to a link returning the image as a file and it will show the save option automatically instead of navigate to another page.

On the server side, specify the mime type as application/octect-stream

Solution 2:

Update - Related to comments in the question

You may also find you need to include

Response.AddHeader("Content-Length", LenB(yourbinary))

To stop some browsers from failing to validate the payload. Also it's important the the ContentType matches what is sent if you are unsure use

Response.Content = "application/octet-stream"

There is no way to initiate the Save As Dialog from the browser via javascript but you can fake the browser into displaying the Save As dialog by setting the attachment value in the Content-Disposition HTTP header.

The way I've tackled this is use a ASP page to generate the image (via COM component, ADODB.Stream, database blob etc) that way you can use;

Response.AddHeader("Content-Disposition", "attachment;filename=myimage.jpg")

This will force the image to be saved rather than displayed inline. So with a script like this you can pass one querystring value to it to display inline (when viewing the image) and one to force it as an attachment which will force the Save As dialog (browser behaviour may be slightly different).

Streaming to the Browser

Using the ADODB.Stream object to output files to the browser.

In the past I've initiated the Save As dialog using javascript (but there is nothing stopping you using a HTML anchor tag to do the same thing without any javascript);

/* 
passing myimageid is a way of identifying the image to return 
be it a database or file system lookup.
*/window.location.href = "/myimage.asp?id=myimageid&display=attachment";

Because the response comes back as an attachment the location is never actually changed and the Save As dialog is displayed immediately with the name of the file passed from the Content-Disposition HTTP header in the file name box.

Post a Comment for "Open "save As" Dialog Box To Download Image"