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
) - 适当的HTTP响应被发送给用户(特别是内容类型:video/mp4)
- and a video file will automatically begin downloading.
- 视频文件会自动开始下载。
I have seen something of the sort with PHP, but is it possible only with HTML/JavaScript?
我在PHP中见过类似的东西,但是只有在HTML/JavaScript中才能实现吗?
3 个解决方案
#1
2
Automatically will depend a lot on the browser and its options. But you can tell the browser what you want to have happen (which it will then double-check with the user) via the Content-Disposition
header in the response. For instance, setting it to attachment;filename=blah.mp4
will, 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.)
自动将很大程度上依赖于浏览器及其选项。但是您可以通过响应中的内容配置头告诉浏览器您希望发生什么(然后它将与用户再次检查)。例如,将它设置为附件;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 Header
directive.
如果不使用服务器端脚本(如您所说的不使用服务器端脚本),您可以在web服务器中通过配置来设置报头。例如,使用Apache时,您将使用匹配这些视频文件的URL的规则并使用头指令。
#2
41
you can use the download attribute
您可以使用download属性
<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
#3
2
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头进行操作。如果您想要正确操作,请使用内容配置头(带有附件值)。
You can set this header using server-side JavaScript or (pretty much) any other server side environment.
您可以使用服务器端JavaScript或任何其他服务器端环境来设置这个头。
#1
2
Automatically will depend a lot on the browser and its options. But you can tell the browser what you want to have happen (which it will then double-check with the user) via the Content-Disposition
header in the response. For instance, setting it to attachment;filename=blah.mp4
will, 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.)
自动将很大程度上依赖于浏览器及其选项。但是您可以通过响应中的内容配置头告诉浏览器您希望发生什么(然后它将与用户再次检查)。例如,将它设置为附件;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 Header
directive.
如果不使用服务器端脚本(如您所说的不使用服务器端脚本),您可以在web服务器中通过配置来设置报头。例如,使用Apache时,您将使用匹配这些视频文件的URL的规则并使用头指令。
#2
41
you can use the download attribute
您可以使用download属性
<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
#3
2
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头进行操作。如果您想要正确操作,请使用内容配置头(带有附件值)。
You can set this header using server-side JavaScript or (pretty much) any other server side environment.
您可以使用服务器端JavaScript或任何其他服务器端环境来设置这个头。