
使用 Ajax 检索并打开 PDF 文件
要通过 Ajax 下载并显示由操作类生成的 PDF 文件,以下方法可以使用:
在操作类中,确保内容类型正确设置为“application/pdf”,并在“contentDisposition”属性中指定所需的文件名:
1 2 3 4 5 6 7 8 9 10 | <code class = "java" > public String execute() {
...
...
File report = signedPdfExporter.generateReport(xyzData, props);
inputStream = new FileInputStream(report);
contentDisposition = "attachment=\"" + report.getName() + "\"" ;
contentType = "application/pdf" ;
return SUCCESS;
}</code>
|
登录后复制
在 Ajax 调用中,配置请求以有效处理流响应:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <code class = "javascript" >$.ajax({
type: "POST" ,
url: url,
data: wireIdList,
cache: false,
success: function (data) {
var blob = new Blob([data]);
var link = document.createElement( 'a' );
link.href = window.URL.createObjectURL(blob);
link.download = "filename_with_extension.pdf" ;
link.click();
window.URL.revokeObjectURL(blob);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert( 'Error occurred while opening fax template' + getAjaxErrorString(textStatus, errorThrown));
}
});</code>
|
登录后复制
通过合并此方法,可以使用 Ajax 成功下载并打开由操作类生成的 PDF 文件。
以上是如何使用 Ajax 下载并打开 Action 类生成的 PDF 文件?的详细内容。更多信息请关注PHP中文网其他相关文章!