下载通过JQuery AJAX Post不工作 [英] Downloading Via JQuery AJAX Post not working
问题描述
这里是我正在使用的PHP代码。
if(file_exists($ file)){
header(Cache-Control:public);
header(Content-Description:File Transfer);
header(Content-Disposition:attachment; filename = $ file);
header(Content-Type:application / zip);
header(Content-Transfer-Encoding:binary);
readfile($ file);
}
这是jQuery。
$ b $($ {code $ $ $''''''''''''''''''''''''''''''''''''''' b $ b $ .ajax({
type:'POST',
url:App / Ajax / Excel / Download.php,
data:formData
});
});
是不是可以这样下载?我尝试了谷歌搜索,有人建议这样做是一样的。但是在我的控制台中,它显示一些垃圾值作为响应。
我在哪里出错?
谢谢
您可以简单地向您的 App / Ajax / Excel / Download发送POST请求。 php
页面,您希望使用的表单数据,并忘记使用jQuery的AJAX。如果您的回复标头是正确的,当您发布erp_customerinvoice表单时,默认情况下,浏览器将显示一个下载文件对话框,您将不会浏览到Download.php页面,因为响应标头将阻止重定向。 / p>
查看一些相关问题以获得更多的解释和替代方案:
i want to download a file using ajax via post request using jQuery.
here is the PHP code i am using.
if (file_exists($file)) {
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
readfile($file);
}
and this is the jQuery.
$('button.erp_ci_download').click(function(){
var formData = $('form#erp_customerinvoice').serialize();
$.ajax({
type: 'POST',
url: "App/Ajax/Excel/Download.php",
data: formData
});
});
is it not possible to download it this way? i tried googling and some suggested it is the same way i am doing it. but in my console it shows some garbage values as response.
where am i going wrong?
thank you..
You could simply send a POST request to your App/Ajax/Excel/Download.php
page with the form data you wish and forget about using jQuery's AJAX. If your response headers are correct, when you post your "erp_customerinvoice" form, the browser will, by default, show you a download file dialog and you will never navigate to the Download.php page because the response headers will prevent a redirect.
See some related questions for more explanation and alternatives:
这篇关于下载通过JQuery AJAX Post不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!