【发布时间】:2022-01-11 12:11:36
【问题描述】:
当加载超过 600MB 的文件时,它会变成空的。后端完全卸载文件。 由 Axios 使用。 使用 downloadjs lib 进行下载。 我用了很多喜欢downloadjs的lib,结果都是一样的。 如何实现使用axios上传大文件?
回复
{data: 'Id;Email;ClientDwhId;Number;16_int;22_datetime;22… headers: {…}, cancelTokenSource: {…}}
cancelTokenSource:
cancel: ƒ cancel(message)
token: CancelToken
promise: Promise
[[Prototype]]: Promise
[[PromiseState]]: "pending"
[[PromiseResult]]: undefined
[[Prototype]]: Object
[[Prototype]]: Object
data: "Id;Email;ClientDwhId;Number;16_int;22_datetime;2
headers:
content-disposition: "attachment; filename=File.csv; filename*=UTF-8''File.csv"
content-length: "42277"
content-type: "text/csv"
[[Prototype]]: Object
[[Prototype]]: Object
import * as contentDisposition from 'content-disposition';
import * as downloadjs from 'downloadjs';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { ApiResponse } from '../api';
const defaultContentType = 'text/plain';
const defaultFilename = 'file.txt';
const downloadFile = (response: ApiResponse<BlobPart>, filename?: string) => {
//create filename
if (!filename) {
const parsedContentDisposition =
response.headers['content-disposition'] &&
contentDisposition.parse(response.headers['content-disposition']);
filename =
parsedContentDisposition && parsedContentDisposition.parameters
? parsedContentDisposition.parameters.filename
: defaultFilename;
}
//create content type
const contentType = response.headers['content-type']
? response.headers['content-type']
: defaultContentType;
//function from downloadjs library
downloadjs(
new Blob([response.data]), filename, contentType,);
};
export const downloadFileFromStream = (filename?: string) => (
stream$: Observable<ApiResponse<BlobPart>>,
) => stream$.pipe(tap(response => downloadFile(response, filename))); //run downloadFile function after response
【问题讨论】:
-
听起来你正在达到 V8 的最大字符串长度,使用块可能会有所帮助。
标签: javascript blob