Angular 7 下载 blob 时文件损坏
Angular 7 get corrupted file while downloading blob
使用 Angular 7,我通过发布 url 文件来调用 api,并尝试使用 fileSaver 库中的 'saveAs' 函数下载它。
文件正在下载,但无法打开,因为它已损坏。
我的调用如下:
var file_url = (response as any).headers['Location'] + 'files/Data.xlsx';
var filename = 'Data_' + this.getDateService.getDateFile() + '.xlsx';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded'
}),
responseType: 'arraybuffer',
observe: 'response'
};
let downloadParameters = { filename: 'Data_' + this.getDateService.getDateFile() + '.xlsx', file: file_url }
this.downloadFileService.downloadFile(downloadParameters, httpOptions).subscribe(reponse => {
var blob = new Blob([(response as any).body], { type: 'application/vnd.openxmlformat-officedocument.spreadsheetml.sheet' });
saveAs(blob, filename);
})
我试过的:
- 切换 MIME 类型 application/vnd.openxmlformat-officedocument.spreadsheetml.sheet by application/octet-stream
- 将响应类型 arraybuffer 切换为 blob 或 blob 为 json
下面,来自服务的响应headers:
响应中存在文件 body:
你们有什么线索吗?
这样试试:
this.downloadFileService.downloadFile(downloadParameters, { responseType: 'blob' }).subscribe(blob=> {
saveAs(blob, filename);
})
使用 Angular 7,我通过发布 url 文件来调用 api,并尝试使用 fileSaver 库中的 'saveAs' 函数下载它。 文件正在下载,但无法打开,因为它已损坏。
我的调用如下:
var file_url = (response as any).headers['Location'] + 'files/Data.xlsx';
var filename = 'Data_' + this.getDateService.getDateFile() + '.xlsx';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded'
}),
responseType: 'arraybuffer',
observe: 'response'
};
let downloadParameters = { filename: 'Data_' + this.getDateService.getDateFile() + '.xlsx', file: file_url }
this.downloadFileService.downloadFile(downloadParameters, httpOptions).subscribe(reponse => {
var blob = new Blob([(response as any).body], { type: 'application/vnd.openxmlformat-officedocument.spreadsheetml.sheet' });
saveAs(blob, filename);
})
我试过的:
- 切换 MIME 类型 application/vnd.openxmlformat-officedocument.spreadsheetml.sheet by application/octet-stream
- 将响应类型 arraybuffer 切换为 blob 或 blob 为 json
下面,来自服务的响应headers:
响应中存在文件 body:
你们有什么线索吗?
这样试试:
this.downloadFileService.downloadFile(downloadParameters, { responseType: 'blob' }).subscribe(blob=> {
saveAs(blob, filename);
})