我正面临一个新问题:我想下载EXCEL文件,我在反应环境中使用GET请求获取该文件。
后端控制器是:
@RequestMapping(method = RequestMethod.GET, value = "/api/tickets/xls")
@ResponseStatus(HttpStatus.OK)
public void getEntitiesXLS(HttpServletRequest request, HttpServletResponse response) throws Exception{
List<Ticket> data = ticketRepository.findAll();
try(
Workbook workbook = new XSSFWorkbook();
){
createExcel(workbook, data )
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=\"tickets.xlsx\"");
workbook.write(response.getOutputStream());
}
}
客户端部分做出反应:
import FileSaver from 'file-saver'
[...]
downloadTickets = () =>{
const { dispatchGetXLS, dispatchThrowError } = this.props
const { reduxApi } = this.context
dispatchGetXLS(reduxApi, (err,data)=>{
if(err){
dispatchThrowError(err)
return
}
console.log(data) // HERE I SEE AN OUTPUT
const blob = new Blob([data], {type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'})
console.log(blob)
FileSaver.saveAs(blob, "tickets.xlsx")
})
}
当我用记事本打开我的.xlsx文件时,它只显示一行: [对象对象]
结束博客大小为15
出了什么问题?
答案 0 :(得分:0)
const blob = new Blob([data.data],{type:'application / vnd.openxmlformats-officedocument.spreadsheetml.sheet'})
现在blob大小是5896但我还看不到excel文件。 Excel表示它似乎已损坏