Suggest a generated file to download from jQuery post

I have a large form, when the user is allowed to enter many different fields, and when they are completed, I need to send the contents of the form to the server, process it, and then spit out the .txt file containing the processing results to load them. Now I am configured, except for the download part. Setting headers for a response to jQuery.post () does not work. Is there any other way than doing some iframe trick to make this work (a la JavaScript / jQuery to upload a file via POST with JSON data )?

Again, I send the data to the server, processing it, and then I would just like to respond to the result with the headers to bring up the download dialog. I do not want to write the result to disk, offer it for download, and then delete the file from the server.

+6
source share
3 answers

Do not use AJAX. There is no way to cross-browser to force the browser to display the save-as dialog box in JavaScript for some arbitrary amount of data received from the server via AJAX. If you want the browser to interpret the results of the HTTP POST request (in this case, by offering a download dialog), do not issue the request through AJAX.

If you need to perform some kind of validation using AJAX, you will need to perform a two-step process when your validation is done through AJAX, and then the download is started by redirecting the browser to the URL where the .txt file can be found.

+10
source

Found this topic, struggling with a similar problem. In this workaround, I ended up using:

$.post('genFile.php', {data : data}, function(url) { $("body").append("<iframe src='download.php?url="+url+"' style='display: none;'></iframe>"); }); 

genFile.php creates the file at an intermediate location using a randomly generated string for the file name. download.php reads the generated file, sets the MIME type and location (allows you to request the use of a predefined name instead of a random string in the actual file name), returns the contents of the file and clears it, deleting the original file.

[edit] can also share PHP code ...

download.php:

 <?php $fname = "/tmp/".$_GET['url']; header('Content-Type: text/xml'); header('Content-Disposition: attachment; filename="plan.xml"'); echo file_get_contents($fname); unlink ($fname); ?> 

genFile.php:

 <?php $length = 12; $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str = substr( str_shuffle( $chars ), 0, $length ).'.xml'; $fh = fopen(('tmp/'.$str), 'w') or die("can't open file"); fwrite($fh,$_POST["data"]); fclose($fh); echo $str; ?> 
+8
source

Instead of using jQuery .post() you just have to do a regular POST by submitting the form and server reply with the corresponding Content-Encoding and MIME headers. You cannot start loading via post() because jQuery encapsulates the returned data.

However, I see quite frequent use in use:

 $.post('generateFile.php', function(data) { // generateFile builds data and stores it in a // temporary location on the server, and returns // the URL to the requester. // For example, http://mysite.com/getFile.php?id=12345 // Open a new window to the returned URL which // should prompt a download, assuming the server // is sending the correct headers: window.open(data); }); 
+7
source

Source: https://habr.com/ru/post/907418/


All Articles