最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to download file using jquery? - Stack Overflow

programmeradmin4浏览0评论

I have to download a file (pdf/zip/txt ...) using jquery.

What i tried ??

<a href="abc.pdf">click</a>

When i click the link the file is opened in browser.

using 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"');

But i have to use only jquery/javascript..

I have to download a file (pdf/zip/txt ...) using jquery.

What i tried ??

<a href="abc.pdf">click</a>

When i click the link the file is opened in browser.

using 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"');

But i have to use only jquery/javascript..

Share Improve this question edited Jul 2, 2014 at 5:23 Deepu Sasidharan asked Jul 2, 2014 at 5:15 Deepu SasidharanDeepu Sasidharan 5,30911 gold badges45 silver badges99 bronze badges 6
  • This $(a).click(function(e) should be like this $('a').click(function(e). And at the first place, why are you preventing the original behavior and doing the prevented behavior manually..? – Rajaprabhu Aravindasamy Commented Jul 2, 2014 at 5:16
  • 1 Your browser opens zip files inline? – Ry- Commented Jul 2, 2014 at 5:17
  • @RajaprabhuAravindasamy. sorry its a typo..i prevent bcoz only to try with jquery.. – Deepu Sasidharan Commented Jul 2, 2014 at 5:24
  • @false, actually i use pdf file. – Deepu Sasidharan Commented Jul 2, 2014 at 5:25
  • 1 Probably a duplicate of: stackoverflow./questions/3749231/… – Retsam Commented Jul 2, 2014 at 5:26
 |  Show 1 more ment

1 Answer 1

Reset to default 13

You can trigger a download by using the new HTML5 download attribute.

<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).

发布评论

评论列表(0)

  1. 暂无评论