jQuery.post()如何处理Content-Disposition:附件? [英] How does jQuery.post() deal with Content-Disposition: attachment?

查看:743
本文介绍了jQuery.post()如何处理Content-Disposition:附件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Going slightly crazy here. I'm making an Ajax call using jQuery.post as follows:

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

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

I'm returning from print.php (as a test):

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,包括标头在内的数据进行正常处理.但是,如何使浏览器(在这种情况下为Firefox)提示用户保存附件?

The data is coming through fine according to Firebug, including the headers. But how do I get the browser (Firefox, in this instance) to prompt the user to save an attachment?

谢谢.

推荐答案

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

It's simply not possible. AJAX requests never cause user prompts.

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

However, you can simply make the request return e.g. JSON or plaintext containing the URL and then use location.href = ...; to redirect to it; this will result in the prompt you are looking for.

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

If the request always results in a file download, you could also consider using a regular form and not using AJAX at all; if the response triggers a download dialog the previous page will remain in the browser window anyway.

这篇关于jQuery.post()如何处理Content-Disposition:附件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆