浏览器不会自动显示从后端 java HTTP 响应返回的内联文件



我用pdfbox即时创建了一个pdf,做了相应的测试,它的格式很好,我可以保存它,通过电子邮件发送 - 一切都按预期工作。 现在相同的pdf(不保存它),当客户端点击按钮时,我返回它;相应的请求/响应成功,但浏览器(任何)不显示它。

一些背景:

  • 前端的 AngularJS 1.6
  • 泽西岛 1.9 作为 REST API
  • HTTP POST 方法
  • 没有错误
  • 它只是停留在当前页面上

我的代码

final ByteArrayOutputStream pdfStream = (ByteArrayOutputStream) generatePricePDF((Price) services.includeClient(price), null);
StreamingOutput streamingOutput = new StreamingOutput() {
@Override
public void write(OutputStream outputStream) throws IOException, WebApplicationException {
pdfStream.writeTo(outputStream);
pdfStream.flush();
pdfStream.close();
}
};
return Response.ok(streamingOutput, MediaType.valueOf("application/pdf"))
.header("Content-Length", pdfStream.toString().length())
.header("Content-Disposition", "inline; filename=price").build();
}

正如我上面提到的,我可以复制编码的响应:

JVBERi0xLjQKJfbk/N8KMSAwIG9iago8PAovVHlwZSAvQ2F0YWxvZwovVmVyc2lvbiAvMS40Ci9QYWdlcyAyIDAgUgo+PgplbmRvYmoKMiAwIG9iago8PAovVHlwZSAvUGFnZXMKL0tpZHMgWzMgMCBSXQovQ291bnQgMQo+PgplbmRvYmoKMyAwIG9iago8PAovVHlwZSAvUGFnZQovTWVkaWFCb3ggWzAuMCAwLjAgNjEyLjAgNzkyLjBdCi9QYXJlbnQgMiAwIFIKL0NvbnRlbnRzIDQgMCBSCi9SZXNvdXJjZXMgNSAwIFIKPj4KZW5kb2JqCjQgMCBvYmoKPDwKL0xlbmd0aCA0MzEKL0ZpbHRlciAvRmxhdGVEZWNvZGUKPj4Kc3RyZWFtDQp4nO1Xy27bMBC86yv2mByUcvmUiiBA7Ui3nqofIFQ6UKFaqCQDSb++I1J20cfFQG4W4MNydrg7JAcLa9dkH2omltQcMsOCnET4Nbszpryn5luWq9JQbhK4H1qft0PvY0oZQSLi9djRZz+SkMTFRyHwo/2XhqTgYq0Cbs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已查看内容处置 而且标题似乎还可以。

我没有相应地管理响应,这是在 AngularJs 1.x 控制器中添加的代码:

$http.post('rest/processprice', $scope.price, {responseType: "blob"})
.then(function(response) {
var file = new Blob([response.data], {type: "application/pdf"});
var fileURL = URL.createObjectURL(file);
$window.open(fileURL, '_blank');
});

以这种方式管理响应,pdf 文件会自动打开。

相关内容

  • 没有找到相关文章

最新更新