正确的pdf下载返回类型。春天-Axios

时间:2018-10-19 02:26:04

标签: spring-mvc axios

我目前正在尝试创建一个下载文件的网络服务调用。我试过了,它返回了无法打开的损坏的pdf文件。

这是我到目前为止所拥有的:

Java代码:

@RequestMapping(value = "/download/{wellId:.+}/{userId:.+}/{companyId:.+}/{wellName:.+}",
        method = RequestMethod.POST)
public byte[] downloadPDF (@PathVariable("wellId") Integer wellId,
                        @PathVariable("userId") Integer userId,
                        @PathVariable("companyId") Integer companyId,
                        @PathVariable("wellName") String wellName,
                        HttpServletRequest request, HttpServletResponse response, @RequestBody CompletionReportRequest req) throws IOException{


        byte[] b = completionReport.getFinalCompletionSummary(wellId, userId, companyId, req);
        response.setContentLength(b.length);
        return b;
}

Axios

const instance = axios.create({
  baseURL: Utils.getLocation()+ '/' + API_PREFIX,
  timeout: 100000,
  headers: { Authorization: 'Bearer ' + $('#token').val()}
});

instance({
    method: Constants.WSMETHOD_POST,
    url: Constants.WSPATH_COMPLETION + Constants.WSPATH_DASHBOARD + Constants.WSPATH_DOWNLOAD + ref.sh.global.wellId + '/' 
                            + ref.sh.global.userId + '/' 
                            + ref.sh.global.companyId + '/' 
                            + ref.sh.global.wellName,
    data : param,
    responseType: 'arraybuffer',
    onUploadProgress: progressEvent => {
        // Computations
    },
    onDownloadProgress: progressEvent => {
        // Computations
    },
}).then(response => {
    // Create psuedo link to download file
    let blob = new Blob([response.data], { type:"application/pdf" })
    let link = document.createElement('a')
    let ts = new Date().getTime();
    link.href = window.URL.createObjectURL(blob)
    link.download = "FinalCompletionSummary_"+ref.sh.global.wellName+".pdf";
    link.click();
}); 

我是否丢失了会损坏pdf文件的内容? TIA。

0 个答案:

没有答案