开发者

force save dialog for a video link

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>开发者_开发百科


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> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜