如何使用 HTML/JavaScript 强制下载?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2295455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:14:59  来源:igfitidea点击:

How do I force download with HTML/JavaScript?

htmlhttpfiledownloadresponse

提问by Alex1987

I have a link and, if a user clicks it, I need 2 things to happen:

我有一个链接,如果用户点击它,我需要发生两件事:

  • A proper HTTP response is sent to the user (especially with Content-Type: video/mp4)
  • and a video file will automatically begin downloading.
  • 向用户发送正确的 HTTP 响应(尤其是使用Content-Type: video/mp4
  • 并且视频文件将自动开始下载。

I have seen something of the sort with PHP, but is it possible only with HTML/JavaScript?

我在 PHP 中看到过类似的东西,但是否只能使用 HTML/JavaScript?

采纳答案by T.J. Crowder

Automaticallywill depend a lot on the browser and its options. But you can tell the browser what you wantto have happen (which it will then double-check with the user) via the Content-Dispositionheader in the response. For instance, setting it to attachment;filename=blah.mp4will, on most browsers, invite the user to download it (using that filename) even if normally the browser would have tried to display/play the content in its own interface. See the link for details. (Downloading is probably the default for mp4 files, but that's up to the user; I find this helpful when offering download links for HTML files.)

自动将在很大程度上取决于浏览器及其选项。但是您可以通过响应中的标头告诉浏览器您想要发生什么(然后它会与用户进行双重检查)Content-Disposition。例如,将其设置为attachment;filename=blah.mp4将在大多数浏览器上邀请用户下载它(使用该文件名),即使浏览器通常会尝试在其自己的界面中显示/播放内容。有关详细信息,请参阅链接。(下载可能是 mp4 文件的默认设置,但这取决于用户;我发现这在提供 HTML 文件的下载链接时很有帮助。)

You can set the header via configuration in your web server if you're not using server-side scripting (as you've said you're not). For instance, with Apache you'd use a rule matching the URL for these video files and use the Headerdirective.

如果您不使用服务器端脚本(正如您所说的那样),您可以通过 Web 服务器中的配置来设置标头。例如,对于 Apache,您将使用与这些视频文件的 URL 匹配的规则并使用Header指令

回答by Bruce Aldridge

you can use the download attribute

您可以使用下载属性

<a href="http..." download></a>

or specify a filename using

或使用指定文件名

<a href="http..." download="filename.pdf"></a>

see more: https://developer.mozilla.org/en/HTML/element/a#attr-download

查看更多:https: //developer.mozilla.org/en/HTML/element/a#attr-download

回答by Quentin

No, this is not possible (at least for values of JavaScript limited to client-side JavaScript).

不,这是不可能的(至少对于仅限于客户端 JavaScript 的 JavaScript 值)。

If you want to override the default behavior of how a browser handles an HTTP resource, you need to do it with HTTP headers. If you want to do it correctly, use the content-disposition header(with an attachment value).

如果要覆盖浏览器如何处理 HTTP 资源的默认行为,则需要使用 HTTP 标头来实现。如果您想正确执行此操作,请使用content-disposition 标头(带有附件值)。

You can set this header using server-side JavaScriptor (pretty much) any other server side environment.

您可以使用服务器端 JavaScript或(几乎)任何其他服务器端环境设置此标头。