Javascript 点击链接后强制浏览器保存文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11353425/
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
Force a browser to save file as after clicking link
提问by Hymanson Henley
Possible Duplicate:
Is it possible to initiate a download prompt in the browser for recognized MIME types using only JavaScript (client-side approach)?
Is there a way to force a browser with a JavaScript routine to save a file as ("save as") after clicking a link? I need for images to be downloaded directly by default and not rendered by the browser.
有没有办法在单击链接后强制使用 JavaScript 例程的浏览器将文件另存为(“另存为”)?我需要默认情况下直接下载图像而不是由浏览器呈现。
回答by Austin
You could use the HTML5 download attribute like so:
您可以像这样使用 HTML5 下载属性:
<a href="path/to/file" download>Click here to download</a>
This opens a "save as" dialog regardless of file type without taking you away from the page.
无论文件类型如何,这都会打开一个“另存为”对话框,而不会带您离开页面。
回答by M Somerville
If you control the server, then you should set it up to send a Content-Disposition: attachment header for the files you wish to be downloaded (e.g. you can do this in an .htaccess file to make all .jpegs in a particular directory).
如果您控制服务器,那么您应该将其设置为发送您希望下载的文件的 Content-Disposition: 附件标头(例如,您可以在 .htaccess 文件中执行此操作以将所有 .jpegs 放在特定目录中) .