下载后损坏的XLSX文件 [英] Corrupt XLSX file after downloading

查看:68
本文介绍了下载后损坏的XLSX文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试下载从角度前端在Java后端中生成的xlsx文件,并且我将其作为带有Content-Disposition标头的附件下载,并且可以使用以下js下载文件代码,但是当我尝试打开它时,它总是损坏

I'm trying to download a xlsx file which is generated in the java backend from the angular frontend and I'm getting file as an attachment with Content-Disposition header and I'm able to download the file with the below js code, but when I try to open it, it's always corrupted

var data = response; //from server
var blob = new Blob([data], { type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml;charset=UTF-8"});
var link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'filname.xlsx';
link.click();

如果我通过控制台记录服务器响应,这就是我发现的内容

If I console log the server response, here is what I found

当我尝试打开下载的文件时,请参见下图

When I try to open the downloaded file, see below image

有人可以帮我吗?

推荐答案

我在AngularJS 1.6.1中遇到了同样的问题.当我从浏览器或 window.open("url")调用HTTP GET服务时,xlsx文件已完美下载:304628字节.但是,AngularJS提供的 response.data 具有289414字节,而Blob包装器具有550963字节,这是作为损坏的文件下载的.如果我以zip格式返回xlsx,也会发生相同的行为.

I had this same issue with AngularJS 1.6.1. When I called my HTTP GET service from the browser or from window.open("url"), the xlsx file was downloaded perfectly: 304628 bytes. However, the response.data provided by AngularJS instead had 289414 bytes and the Blob wrapper had 550963 bytes, which is what is downloaded as a corrupted file. This same behavior occurred if I return the xlsx in a zip.

我通过将 XMLHttpRequest.responseType 属性设置为这样来解决此问题:

I solved this by setting the XMLHttpRequest.responseType property as such:

$http.get(url, {responseType:'arraybuffer'});

这篇关于下载后损坏的XLSX文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆