我有一个生成 PDF 的动作类。 contentType
已适当设置。
public class MyAction extends ActionSupport
{
public String execute() {
...
...
File report = signedPdfExporter.generateReport(xyzData, props);
inputStream = new FileInputStream(report);
contentDisposition = "attachment=\"" + report.getName() + "\"";
contentType = "application/pdf";
return SUCCESS;
}
}
我通过 Ajax 调用将其称为 action
。我不知道将此流传送到浏览器的方式。我尝试了几件事,但没有任何效果。
$.ajax({
type: "POST",
url: url,
data: wireIdList,
cache: false,
success: function(response)
{
alert('got response');
window.open(response);
},
error: function (XMLHttpRequest, textStatus, errorThrown)
{
alert('Error occurred while opening fax template'
+ getAjaxErrorString(textStatus, errorThrown));
}
});
以上给出了错误:
您的浏览器发送了该服务器无法理解的请求。
原文由 Nayn 发布,翻译遵循 CC BY-SA 4.0 许可协议
为此,您不一定需要 Ajax。如果在服务器端代码
attachment
设置为content-disposition
,只需一个<a>
链接就足够了。这样父页面将保持打开状态,如果这是您的主要关注点(否则为什么您会不必要地选择 Ajax?)。此外,没有办法很好地异步处理这个问题。 PDF不是字符数据。是二进制数据。你不能做像$(element).load()
这样的事情。你想为此使用 全新 的请求。为此<a href="pdfservlet/filename.pdf">pdf</a>
非常合适。为了帮助您更多地使用服务器端代码,您需要详细说明所使用的语言并发布代码尝试的摘录。