javascript - Handle file download from ajax post -
i have javascript app sends ajax post requests url. response might json string or might file (as attachment). can detect content-type , content-disposition in ajax call, once detect response contains file, how offer client download it? i've read number of similar threads here none of them provide answer i'm looking for.
please, please, please not post answers suggesting shouldn't use ajax or should redirect browser, because none of option. using plain html form not option. need show download dialog client. can done , how?
edit:
apparently, cannot done, there simple workaround, suggested accepted answer. comes across issue in future, here's how solved it:
$.ajax({ type: "post", url: url, data: params, success: function(response, status, request) { var disp = request.getresponseheader('content-disposition'); if (disp && disp.search('attachment') != -1) { var form = $('<form method="post" action="' + url + '">'); $.each(params, function(k, v) { form.append($('<input type="hidden" name="' + k + '" value="' + v + '">')); }); $('body').append(form); form.submit(); } } });
so basically, generate html form same params used in ajax request , submit it.
create form, use post method, submit form - there's no need iframe. when server page responds request, write response header mime type of file, , present download dialog - i've done number of times.
you want content-type of application/download - search how provide download whatever language you're using.
Comments
Post a Comment