1

I'm trying to make my Download function popup with the file to download, it is outputting the binary of the file to the div when i click the link, how can i make it so that when i click the link, it will instead ask me to download it? I know i can do it with querystring and using headers in php, but can i do it with ajax/javascript in a similar way? thanks, here's what i tried:

<html> <head> <script> function Download(plan_name) { if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { var resp = xmlhttp.responseText; document.getElementById("txtHint").innerHTML=resp; //HOW TO SHOW test.zip in a save-as dialog? } } xmlhttp.open("GET","fetcher.php?file=/raid0/data/naswebsite/Projects/Projects/07-003_Dawson_Mine/Flight\ Plans/Dawson_Sth_1211_AMG_700.zip"); xmlhttp.send(); } </script> </head> <body> <a href="#" onClick="Download();">Test Download</a> <div id="txtHint"></div> </body> </html> 

1 Answer 1

2

It is not possible to download a file from an AJAX request.
Instead, you can load the URL in a hidden <iframe>.

Sign up to request clarification or add additional context in comments.

1 Comment

Or do what most people do and open a new window with window.open and set its target URL, and have the response use the header Content-Disposition: attachment; filename=whatever and/or Content-Type: application/force-download

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.