www.webdeveloper.com
Results 1 to 7 of 7

Thread: How do I force an open/save dialogue on a download?

  1. #1
    Join Date
    May 2006
    Posts
    9

    How do I force an open/save dialogue on a download?

    Hi,

    Basically i've got a pdf file that I want people to access as a download. When you click on it, it will automatically open the file for you. But I want it to display the open/save dialogue that you get when downloading a zip or unknown file type.

    It is for a website that I am making for my church, so I want to make it as easy to use as possible. So I don’t to put the file in a zip, or expect people to know how to right click and select ‘save target as’.

    Is there an easy way of doing this? Perhaps something to do with target= something?

    Thanks for any help you can give!

    Neil.

  2. #2
    Join Date
    Apr 2006
    Posts
    370
    Can be done with (for example) PHP by sending header information. Not sure it can be done with pure HTML, though.

  3. #3
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    The easy way is to zip it up. Nothing can be done in HTML to force and even sending headers may not work in IE.
    "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." Brian W. Kernighan

  4. #4
    Join Date
    Apr 2006
    Location
    Houston
    Posts
    1,374
    I did some quick Googling and there appears to be 2 different ways to do this. (I have not tried either). One is a JavaScript approach which looks to be IE specific
    Code:
    <html>
    <body>
    <script>
     function downloadme(x){
        myTempWindow = window.open(x,’’,’left=10000,screenX=10000’);
        myTempWindow.document.execCommand(’SaveAs’,’null’,x);
        myTempWindow.close();
    }
    </script>
    
    <a href=javascript:downloadme("whatever.pdf");>Download this pdf</a>
    </body>
    </html>
    For the other you can force the browser to display a "Save As/Open from current location" dialogue box when it wants to write known binary content to the client.

    The official approach for this is to use a http header to request the display of user permission dialog box. This header is known as "Content-Disposition."

    Following is the sample for server-side based applications, (almost all http libraries provide an API to set an http header).

    HttpResponse.setHeader("Content-Disposition "," attachment; filename= whatever.pdf");

    I beleive you can even force Content-Disposition to fit in a meta tag if you don't have the ability to issue server side commands. Something like:
    Code:
    <META HTTP-EQUIV="Content-Disposition" CONTENT="attachment; filename=whatever.pdf">

  5. #5
    Join Date
    Nov 2002
    Location
    XYZZY - UK
    Posts
    1,760
    Apache .htaccess: ForceType application/octet-stream pdf

  6. #6
    Join Date
    May 2006
    Posts
    9
    Thankyou, i'll try it soon.

  7. #7
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    Even with an application/octet-stream header IE may decide to "sniff" the response content and act according to the user's PDF binding.
    "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." Brian W. Kernighan

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles