【发布时间】:2019-10-25 03:02:24
【问题描述】:
我正在尝试使用来自 ReactJs 的 jQuery ajax 发布请求下载文件。我有中间层的快速路由器,它使用请求 api 将请求发送到 Spring Boot 休息 api。 ajax => express post route => Spring Boot 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)
}
}
}
)
});
// in springboot rest api
@RequestMapping(value = "/downloadCustomerReport", method = RequestMethod.POST)
public ResponseEntity<byte[]> downloadCustomerReport(@RequestBody SamplesDataRequestHolderVO samplesDataRequestVO){
byte[] resource = null;
try(Workbook workbook = new XSSFWorkbook();
ByteArrayOutputStream out = new ByteArrayOutputStream();){
// I have create and attached worksheet
workbook.write(out);
writeToFile(new ByteArrayInputStream(out.toByteArray()));
resource = out.toByteArray();
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.valueOf("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=CR Final.xlsx");
return ResponseEntity.ok()
.headers(headers)
.body(resource);
}
我从 Spring Boot 中保存了文件,没关系。我尝试从 Postman 下载响应。它也很好,文件没有问题。但是当我尝试反应时,它总是被破坏。我在 ajax 中获取数据为
'PK.[.N[Content_Types].xml.TIn1'。
我比较了 ajax 和 Postman 中的响应数据,它们是相同的。如何调试?
【问题讨论】:
标签: jquery reactjs spring-boot express request