javascript 如何使用jquery下载文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24523347/
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
How to download file using jquery?
提问by Deepu Sasidharan
I have to download a file (pdf/zip/txt ...)
using jquery.
我必须下载一个file (pdf/zip/txt ...)
使用jquery.
What i tried ??
我试过什么??
<a href="abc.pdf">click</a>
When i click the link the file is opened in browser.
当我单击链接时,文件在浏览器中打开。
using jquery...
使用 jquery...
$('a').click(function(e)
{
e.preventDefault();
var link = $(this).attr('href');
window.location = link;
});
This also leads to open the file in browser. But i want to download it not display in browser.
这也会导致在浏览器中打开文件。但我想下载它不显示在浏览器中。
In PHP
we can use header('Content-Disposition: attachment; filename="test.mp3"');
在PHP
我们可以使用header('Content-Disposition: attachment; filename="test.mp3"');
But i have to use only jquery/javascript..
但我必须只使用 jquery/javascript..
回答by jontewks
You can trigger a download by using the new HTML5 download attribute.
您可以使用新的 HTML5 下载属性触发下载。
<a href="path_to_file" download="proposed_file_name">Download</a>
path_to_file is either an absolute or relative path, proposed_file_name the filename to save to (can be blank, then defaults to the actual filename).
path_to_file 是绝对路径或相对路径,proposed_file_name 要保存到的文件名(可以为空,然后默认为实际文件名)。