使用ReactJs jQuery ajax post请求下载时Excel文件已损坏

使用ReactJs jQuery ajax post请求下载时Excel文件已损坏,jquery,reactjs,spring-boot,express,request,Jquery,Reactjs,Spring Boot,Express,Request,我正在尝试从ReactJs使用jQuery ajax post请求下载一个文件。我有一个express路由器的中间层,它使用请求api向Spring Boot rest api发送请求。ajax=>express post route=>Spring引导api。文件正在下载,但内容始终损坏 我已经尝试了互联网上提供的所有解决方案,但没有一个适合我 // ajax calling express post route $.ajax({ url: "/downloadRepo

我正在尝试从ReactJs使用jQuery ajax post请求下载一个文件。我有一个express路由器的中间层,它使用请求api向Spring Boot rest api发送请求。ajax=>express post route=>Spring引导api。文件正在下载,但内容始终损坏

我已经尝试了互联网上提供的所有解决方案,但没有一个适合我

// ajax calling express post route
$.ajax({
            url: "/downloadReport",
            contentType: 'application/json',
            data:JSON.stringify({"data1":"value1"}),
            type:'POST',
            cache: true,
            responseType: 'arraybuffer',
            success: function(response) {
                console.log(response);
                var fileName=response.headers['content-disposition'].split('filename=')[1];
                var blob = new Blob([response.data], {type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
                const blobURL = window.URL.createObjectURL(blob);
                const tempLink = document.createElement('a');
                tempLink.style.display = 'none';
                tempLink.href = blobURL;
                tempLink.setAttribute('download', fileName);
                tempLink.click();

            }.bind(this),
            error: function(xhr, status, err) {
                console.log(err)
                console.log(status)
                console.log(xhr)
            }.bind(this)
        })

// express route.
router.post("/downloadReport", (req, response, next) => {
    request(
        {
            url: '/springbootapi/downloadCustomerReport',
            method: "POST",
            body: JSON.stringify(req.body),
            headers: {
                'Content-Type':'application/json'
            },
        }, function (error, res, body) {
            if(error){
                console.log(error);
                response.status(500).send(error);
            }else{
                try {
                    response.send({headers: res.headers, data: res.body})
                }catch(err){
                    response.status(500).send(err)
                }
            }
        }
    )
});
//在springboot rest api中
@RequestMapping(value=“/downloadCustomerReport”,method=RequestMethod.POST)
公共响应下载CustomerReport(@RequestBody SamplesDataRequestHolderVO samplesDataRequestVO){
字节[]资源=null;
尝试(工作簿=新XSSFWORKWORK();
ByteArrayOutputStream out=新建ByteArrayOutputStream();){
//我已经创建并附加了工作表
练习册。写(出);
writeToFile(新的ByteArrayInputStream(out.toByteArray());
resource=out.toByteArray();
}
HttpHeaders=新的HttpHeaders();
headers.setContentType(MediaType.valueOf(“application/vnd.openxmlformats officedocument.spreadsheetml.sheet”);
headers.add(HttpHeaders.CONTENT_处置,“附件;文件名=CR Final.xlsx”);
返回ResponseEntity.ok()
.标题(标题)
.机构(资源);
}
我从Spring Boot保存了这个文件,它还可以。我试着从邮递员那里下载回复。它也很好,文件中没有问题。但当我尝试从react开始时,它总是被破坏。我用ajax获取数据

"PK"�[�N[内容类型].xml�锡1'


我比较了ajax和Postman中的响应数据,它们是相同的。我如何调试它?

我通过在Springboot controller中使用Base64对bytearray进行编码,并将响应体作为编码字符串发送,从而使其正常工作

@RequestMapping(value=“/downloadCustomerReport”,method=RequestMethod.POST)
public ResponseEntity downloadCustomerReport(@RequestBody SamplesDataRequestHolderVO samplesDataRequestVO)引发IOException{
字节[]资源=null;
尝试(工作簿=新XSSFWORKWORK();
ByteArrayOutputStream out=新建ByteArrayOutputStream();){
//我已经创建并附加了工作表
练习册。写(出);
writeToFile(新的ByteArrayInputStream(out.toByteArray());
resource=out.toByteArray();
}
HttpHeaders=新的HttpHeaders();
headers.setContentType(MediaType.valueOf(“应用程序/八位字节流”);
headers.add(HttpHeaders.CONTENT_处置,“附件;文件名=CR Final.xlsx”);
字符串s=Base64.getEncoder().encodeToString(资源);//此处编码了my bytearray
返回ResponseEntity.ok()
.标题(标题)
.机构;
}
然后在前端,我使用了blob util库中的base64StringToBlob函数,并将类型更改为application/xlsx:

var blob = base64StringToBlob(response.data, 'application/xlsx');

我在Springboot控制器中使用Base64对bytearray进行编码,并将响应体作为编码字符串发送,从而使其正常工作

@RequestMapping(value=“/downloadCustomerReport”,method=RequestMethod.POST)
public ResponseEntity downloadCustomerReport(@RequestBody SamplesDataRequestHolderVO samplesDataRequestVO)引发IOException{
字节[]资源=null;
尝试(工作簿=新XSSFWORKWORK();
ByteArrayOutputStream out=新建ByteArrayOutputStream();){
//我已经创建并附加了工作表
练习册。写(出);
writeToFile(新的ByteArrayInputStream(out.toByteArray());
resource=out.toByteArray();
}
HttpHeaders=新的HttpHeaders();
headers.setContentType(MediaType.valueOf(“应用程序/八位字节流”);
headers.add(HttpHeaders.CONTENT_处置,“附件;文件名=CR Final.xlsx”);
字符串s=Base64.getEncoder().encodeToString(资源);//此处编码了my bytearray
返回ResponseEntity.ok()
.标题(标题)
.机构;
}
然后在前端,我使用了blob util库中的base64StringToBlob函数,并将类型更改为application/xlsx:

var blob = base64StringToBlob(response.data, 'application/xlsx');

尝试在您的请求中添加responseType作为“blob”。

尝试在您的请求中添加responseType作为“blob”。

这不会提供问题的答案。一旦您有足够的答案,您将能够;相反,.-这不会提供问题的答案。一旦您有足够的答案,您将能够;相反-