Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download file using jquery?

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

like image 242
Deepu Sasidharan Avatar asked Jul 02 '14 05:07

Deepu Sasidharan


1 Answers

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

like image 180
jontewks Avatar answered Oct 14 '22 23:10

jontewks