Note that there are some explanatory texts on larger screens.

plurals
  1. POHandle file download from ajax post
    primarykey
    data
    text
    <p>I have a javascript app that sends ajax POST requests to a certain URL. Response might be a JSON string or it might be a file (as an attachment). I can easily detect Content-Type and Content-Disposition in my ajax call, but once I detect that the response contains a file, how do I offer the client to download it? I've read a number of similar threads here but none of them provide the answer I'm looking for. </p> <p>Please, please, please do not post answers suggesting that I shouldn't use ajax for this or that I should redirect the browser, because none of this is an option. Using a plain HTML form is also not an option. What I do need is to show a download dialog to the client. Can this be done and how?</p> <p>EDIT:</p> <p>Apparently, this cannot be done, but there is a simple workaround, as suggested by the accepted answer. For anyone who comes across this issue in the future, here's how I solved it:</p> <pre><code>$.ajax({ type: "POST", url: url, data: params, success: function(response, status, request) { var disp = request.getResponseHeader('Content-Disposition'); if (disp &amp;&amp; disp.search('attachment') != -1) { var form = $('&lt;form method="POST" action="' + url + '"&gt;'); $.each(params, function(k, v) { form.append($('&lt;input type="hidden" name="' + k + '" value="' + v + '"&gt;')); }); $('body').append(form); form.submit(); } } }); </code></pre> <p>So basically, just generate a HTML form with the same params that were used in AJAX request and submit it. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload