Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript file download

I need to download a file (test.xml) and allow/prompt user to save the file on click on download button. The file resides in url "http://localhost/test/test.xml" .

I have added html code

<input type=button value="Download" onclick='javascript:download()/>

and javascript code is

function download() {
   var url = "http://localhost/test/test.xml";
       window.open(url, 'Download');
}

But this opens the page in new window. How do I prompt to download and save the file. Any inputs will be of help. Thanks

like image 668
User Avatar asked Mar 08 '26 00:03

User


1 Answers

You have to change the content type in the header. You need to do some server scripting or configurate your webserver.

I Googled a link that will help you in the right direction: http://www.boutell.com/newfaq/creating/forcedownload.html

like image 61
Simon Edström Avatar answered Mar 09 '26 14:03

Simon Edström



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!