Wurstbro
Wurstbro

Reputation: 974

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 e.g. sending the data with AJAX and POST to the server?

Upvotes: 1

Views: 603

Answers (2)

syl.fabre
syl.fabre

Reputation: 746

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

Upvotes: 0

SLaks
SLaks

Reputation: 888223

You cannot use AJAX to download a file.

Instead, you should let the form submit normally.

Upvotes: 1

Related Questions