简体   繁体   English

如何使用jquery下载文件?

[英]How to download file using jquery?

I have to download a file (pdf/zip/txt ...) using jquery.我必须使用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..

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 要保存到的文件名(可以为空,然后默认为实际文件名)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM