1

I want to create a download link for a video. How I can force to open save as dialog box when a link is clicked.

<a href="#" onclick="downloadVideo()">Download Video</a>
coure2011
  • 40,286
  • 83
  • 216
  • 349
  • What does `downloadVideo()` do and what type is the video? Do you have access to the configuration of the server serving the video? – Pekka Jul 02 '11 at 07:48
  • please reffer this post on SO http://stackoverflow.com/questions/2897619/using-html5-javascript-to-generate-and-save-a-file i hope will help you – Devjosh Jul 02 '11 at 07:49

1 Answers1

1

As Pekka implied, you force a file download from a normal HTML link by sending an additional HTTP header. This means that you may need to change the configuration of your web server to get it to work.

Normally, clicking a link won't return a Content-Disposition header; setting that is how you get a "Save file..." dialog box to appear. The response headers would look a bit like:

HTTP/1.1 200 OK
Date: Sun, 21 Aug 2011 11:45:59 GMT
Server: Apache/2.2.14 (Ubuntu)
Content-Disposition: attachment; filename=video.m4v;

Without knowing the inner workings of your application or site, I'd tentatively suggest (for the benefit of users with JavaScript switched off) you don't use JS for this if possible.

Your best bet is to make a change to the configuration of your web server. With Apache, you'd do something like:

<FilesMatch "\.m4v$">
  <IfModule mod_headers.c>
    Header set Content-Disposition "attachment"
  </IfModule>
</FilesMatch>

...which will make all M4V files show a "Save file..." dialog box. Or if your application was written in PHP, you could do:

header('Content-Disposition: attachment; filename="video.m4v"');

This page has a bit of JavaScript (possibly JScript?) you could use that works in Internet Explorer, but not in any other browser:

<a href="javascript:void(0);" onclick="document.execCommand('SaveAs',true,'video.m4v');">Download video</a> 
alexmuller
  • 2,207
  • 3
  • 23
  • 36