jquery ajax post response

I ran into a problem, I send data using $.ajax(), I get the generated pdf in response, but I receive and see its contents. How can I turn this crap into a file download or open it in a browser without reloading the source page?

Either there is a second option, which I went for, but ran into the problem of a too long URI, since the data sent using GET is too large

window.open("report.php?"+ s);

The question is how to convert the overlying structure into POST?


Answer 1, authority 100%

can simply get the url to the file in the response, for example site.dev/file.pdf , and then execute window.location.href = url_from_ajax, the browser will offer to download or open the document


Answer 2

where in PHP where you process the request, write in the output

header("Cache-Control: ");
header("Content-type: application/pdf ");
header("Content-Description: File Transfer");
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename="'.   .'"');

I just don’t remember, I need to display something after the Last Header, otherwise it should work and the user will open a window and offer to download the file from the server.


Answer 3

Hmm, why ajax ? If a header comes from the server with the “file” type, then don’t worry, the user will not get to the page with the contents of the file :))) => Your bike that you are assembling will have square wheels, and instead of a saddle – a seat :)))

It’s simple, the architecture is as follows:

  1. php script.
  2. tmp folder

php script functions:

  1. Generate the content to a file and save it to the tmp folder.

  2. Transmitting content with titles that Shrekdescribed.

  3. Delete the file after the transfer is complete.

File transfer can be done using CURL. Those. after curl_close() we delete the file so it doesn’t take up space in tmp.

That’s the whole architecture πŸ™‚

As far as your JS is concerned… submit the request and return false; πŸ™‚