Vue.js Axios PDF下载打破了PDF格式
目前,我在使用axios的vue.js中遇到了一个奇怪的行为。我的目标是提供一个pdf的下载链接,其中总结了用户的数据。作为参数,我传递用户的生日和唯一id。使用这两个参数,我查找数据,使用pdfbox生成pdf,并通过jax-rs提供生成的pdf。现在,如果我通过axios下载pdf,pdf的格式将完全消失。pdf中仅包含替换的值。其他一切都失去了。但是,如果我只使用普通的,您是否可以尝试更改Vue.js Axios PDF下载打破了PDF格式,vue.js,axios,jax-rs,pdfbox,java-ee-8,Vue.js,Axios,Jax Rs,Pdfbox,Java Ee 8,目前,我在使用axios的vue.js中遇到了一个奇怪的行为。我的目标是提供一个pdf的下载链接,其中总结了用户的数据。作为参数,我传递用户的生日和唯一id。使用这两个参数,我查找数据,使用pdfbox生成pdf,并通过jax-rs提供生成的pdf。现在,如果我通过axios下载pdf,pdf的格式将完全消失。pdf中仅包含替换的值。其他一切都失去了。但是,如果我只使用普通的,您是否可以尝试更改response.setContentType(“application/pdf”)torespons
response.setContentType(“application/pdf”)
toresponse.setContentType(“应用程序/八位字节流”)代码>?我刚试过。然而,这并没有解决问题。嗯,也许你可以用响应编辑你的问题。数据看起来像什么?您还可以在创建blob时传递该类型,如:newblob([response.data],{type:'application/pdf'})
我已经尝试添加该类型,但它没有更改任何内容。response.data是一个pdf文件,因此它是神秘的,不会有帮助。
@GET
public Response getApplication(//
@QueryParam("applicationId") String applicationId,//
@QueryParam("bd") Long birthTime,//
@Context HttpServletResponse response)
throws IOException, URISyntaxException {
Date birthDate = new Date(birthTime);
ApplicantAuthentication authentication = new ApplicantAuthentication(applicationId, birthDate);
boolean permissionForApplication = permissionUsecase.checkApplicantsPermission(authentication);
if (!permissionForApplication) {
return Response//
.status(Response.Status.UNAUTHORIZED)//
.build();
}
response.setHeader("Content-Disposition", "attachment; filename=hello.pdf");
response.setContentType("application/pdf");
ServletOutputStream outStream = response.getOutputStream();
summaryUsecase.createPdfSummary(applicationId, outStream);
outStream.flush();
return Response//
.status(Response.Status.OK)//
.build();
}
<a :href="pdfUrl">...
computed: {
pdfUrl: function () {
return "/rest/summary/pdf?applicationId="+this.applicationId+"&bd="+this.bd;
}
}
<a href="#" @click.prevent="downloadPdf()">...
methods: {
downloadPdf() {
RetiremenHomeService.retrieveSummaryPdf(this.applicationId, this.bd).then((response) => {
const link = document.createElement('a');
link.href = URL.createObjectURL(new Blob([response.data]));
link.download = "hello.pdf";
link.click();
URL.revokeObjectURL(link.href);
});
}
},
static retrieveSummaryPdf(){
return axios.get(`${RetirementHomeService.serviceUrlPrefix}/summary/pdf`);
}