gpt4 book ai didi

php - jQuery.post() 如何处理 Content-Disposition : attachment?

转载 作者:行者123 更新时间:2023-12-01 07:25:45 25 4
gpt4 key购买 nike

这里有点疯狂。我正在使用 jQuery.post 进行 Ajax 调用,如下所示:

  $.post('php/print.php',{data:dS},function(res){...  },"text");

我从 print.php 返回(作为测试):

header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=test.doc");

echo "<html>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">";
echo "<body>";
echo "Testing-2-3!";
echo "</body>";
echo "</html>";

根据 Firebug,数据传输良好,包括 header 。但是如何让浏览器(在本例中为 Firefox)提示用户保存附件?

谢谢。

最佳答案

这根本不可能。 AJAX 请求永远不会引起用户提示。

但是,您可以简单地使请求返回,例如包含 URL 的 JSON 或纯文本,然后使用 location.href = ...; 重定向到它;这将导致您正在寻找的提示。

如果请求总是导致文件下载,您也可以考虑使用常规表单,而根本不使用 AJAX;如果响应触发下载对话框,则上一页仍将保留在浏览器窗口中。

关于php - jQuery.post() 如何处理 Content-Disposition : attachment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9718102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com