提问人:azh 提问时间:9/28/2023 更新时间:9/28/2023 访问量:44
NodeJS 和 AngularJS 生成的损坏文件
NodeJS and AngularJS genereted corrupted files
问:
我在下载文件时遇到问题,我的数据来自 API (NodeJS + Express)。 xlsx 文件只是损坏了,无法打开,就像 zip 格式的文件一样。 一些 xlsx 有奇怪的字符,例如:
Ӫ E Q^ 3 3 ϾhwR- vY!c O y I 㱣 eG [J Gʌw ES lz e L 'Sr 'x2 R Į ¦
这是我的 NodeJS 代码(将数据返回到前面的方法):
public async downloadFile(patch: string, start: [number, number], timeout: number) {
try {
...
const buffer: any = [];
let readStream;
const downloadPromise = new Promise((resolve, _reject, onCancel) => {
onCancel(() => {
if (readStream) readStream.destroy();
return;
});
// create the read stream and register events callbacks
readStream = this.bucket.file(storageInfo.$path).createReadStream();
readStream.on('error', err => {
readStream.destroy();
resolve({ downloaded: false, data: err });
});
readStream.on('data', data => {
buffer.push(data);
});
readStream.on('end', () => {
readStream.destroy();
resolve({ downloaded: true, data: Buffer.concat(buffer) });
});
});
return Response.success({ file: storageInfo, buffer: result.data });
} catch (ex) {
...
}
}
Angularjs 代码:
public async previewFileFromGoogleStorage(hash: string): Promise<IFileDownload> {
try {
if (!hash) return null;
const operation = await this.$http({
method: 'GET',
url: `${this.getUrlDownload()}${hash}`,
cache: false,
timeout: this.timeout,
responseType: "arraybuffer"
});
const response: angular.IHttpResponse<any> = operation;
const parsedReponse = JSON.parse(new TextDecoder().decode(response.data));
const resultFile = <IDownloadParamsReturn>parsedReponse.data;
const file = resultFile.file;
const buffer = resultFile.buffer.data;
const fileName = file.fileName;
const fileBuffer = new Uint8Array(buffer);
const fileType = this.getContentType(file.fileType);
const fileBlob = new Blob([fileBuffer], { type: fileType });
const fileURL = window.URL.createObjectURL(fileBlob);
const result = <IFileDownload> { url: fileURL, name: fileName, type: fileType, blob: fileBlob };
return result;
} catch (ex) {
HandleError.exception(ex);
}
}
private async sendToDownload(): Promise<void> {
try {
this.$scope.file = <IFileDownload>await this.downloadService.previewFileFromGoogleStorage(hash);
if (this.$scope.file.type.indexOf('image') === 0) {
this.$scope.type = 'image';
this.previewImage();
} else if (this.$scope.file.type === 'application/pdf') {
this.$scope.type = 'pdf';
this.previewPdf();
} else {
this.$scope.type = 'other';
const self = this;
this.$scope.$apply(function () {
self.blockUI.stop();
self.downloadFile();
});
}
} catch (ex) {
this.blockUI.stop();
HandleError.exception(ex);
}
}
private downloadFile(): void {
if (this.$rootScope.isIE) {
window.navigator.msSaveOrOpenBlob(this.$scope.file.blob, this.$scope.file.name);
} else {
let link = document.createElement('a');
link.href = window.URL.createObjectURL(new Blob([this.$scope.file.blob], { type: this.$scope.file.blob.type }));
link.download = this.$scope.file.name;
link.click();
}
}
我被这个问题困住了,我不知道该怎么办,我已经尝试过将内容类型更改为“blob”或干脆不使用它,但没有奏效。
答: 暂无答案
评论