简体   繁体   中英

download a file generated by POST request with JQuery/javascript

Is it possible to show a download dialog after sending POST data to the server?

Since I need many complex values "just" using a HTML form with method post won't work.

Until now I used the GET method the following way:

$("form").submit(function() {
    window.location.href = 'upload.php?url='+$(".url").val()+'&host='+$(".host.clicked").attr("host");      
    return false;
}

which, with location.href will prompt me a nice download dialog, since upload.php returns headers for a file.

The problem now is that I want to give the user the option to upload a file to the form, which would not work with the GET url method I'm using.

So how can I show the download dialog after eg sending the data with AJAX and POST to the server?

You cannot use AJAX to download a file.

Instead, you should let the form submit normally.

upload.php can be the action of a POST form.

  1. User selects a file from his computer
  2. User submits the POST form to upload.php
  3. upload.php does whatever needs to be done
  4. upload.php returns headers for a file
  5. User gets the download dialog

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM