首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使上传速度更快的角度7?

如何使上传速度更快的角度7?
EN

Stack Overflow用户
提问于 2020-06-17 08:04:56
回答 2查看 666关注 0票数 1

我正在努力加快上传速度。所以我尝试了不同的解决方案,包括BackEnd和前端。那些是,

1)我上传了tar文件(已经压缩了一个)

2)我尝试了块上传(顺序),如果响应成功,下一个API将被触发。在后端端,在同一个文件中,内容将被追加。

3)我尝试了块上传,但是并行地,每次我提出50个请求来上传块内容(我知道,在一个时间浏览器只处理6个请求)。从后端端,在接收到最终请求后,我们将所有块文件分别存储,并将所有这些块添加到单个文件中。

但我注意到,我看不出与所有这些情况有多大差别。

以下是我的服务文件

代码语言:javascript
复制
export class largeGeneUpload {

    chromosomeFile: any;
    options: any;
    chunkSize = 1200000;
    activeConnections = 0;
    threadsQuantity = 50;

    totalChunkCount = 0;
    chunksPosition = 0;
    failedChunks = [];


    sendNext() {
        if (this.activeConnections >= this.threadsQuantity) {
            return;
        }

        if (this.chunksPosition === this.totalChunkCount) {
            console.log('all chunks are done');
            return;
        }

        const i = this.chunksPosition;
        const url = 'gene/human';
        const chunkIndex = i;
        const start = chunkIndex * this.chunkSize;
        const end = Math.min(start + this.chunkSize, this.chromosomeFile.size);
        const currentchunkSize = this.chunkSize * i;
        const chunkData = this.chromosomeFile.webkitSlice ? this.chromosomeFile.webkitSlice(start, end) : this.chromosomeFile.slice(start, end);
        const fd = new FormData();
        const binar = new File([chunkData], this.chromosomeFile.upload.filename);
        console.log(binar);
        fd.append('file', binar);
        fd.append('dzuuid', this.chromosomeFile.upload.uuid);
        fd.append('dzchunkindex', chunkIndex.toString());
        fd.append('dztotalfilesize', this.chromosomeFile.upload.total);
        fd.append('dzchunksize', this.chunkSize.toString());
        fd.append('dztotalchunkcount', this.chromosomeFile.upload.totalChunkCount);
        fd.append('isCancel', 'false');
        fd.append('dzchunkbyteoffset', currentchunkSize.toString());

        this.chunksPosition += 1;
        this.activeConnections += 1;
        this.apiDataService.uploadChunk(url, fd)
            .then(() => {
                this.activeConnections -= 1;
                this.sendNext();
            })
            .catch((error) => {
                this.activeConnections -= 1;
                console.log('error here');
                // chunksQueue.push(chunkId);
            });

        this.sendNext();
    }


    uploadChunk(resrc: string, item) {
        return new Promise((resolve, reject) => {
            this._http.post(this.baseApiUrl + resrc, item, {
                headers: this.headers,
                withCredentials: true
            }).subscribe(r => {
                console.log(r);
                resolve();
            }, err => {
                console.log('err', err);
                reject();
            });
        });
}

但问题是,如果我上传相同的文件在谷歌驱动器,这是不需要太多的时间。

让我们考虑一下,我有700 MB的文件,上传它在谷歌驱动器它花了3分钟。但同样的700 MB的文件上传与我的角度代码与我们的后端服务器,它花了7分钟完成它。

如何提高文件上传的性能?

EN

回答 2

Stack Overflow用户

发布于 2020-06-17 08:10:42

请原谅,这似乎是愚蠢的回答,但这取决于您的托管基础设施。

票数 0
EN

Stack Overflow用户

发布于 2020-06-17 08:24:50

很多变量都可能导致这种情况,但根据您的故事,这与您的前端代码无关。将其分成块不会有帮助,因为浏览器有自己的优化算法来上传文件。最有可能的罪魁祸首是后端服务器或客户端到服务器的连接。

你说谷歌的驱动速度很快,但你也应该知道,谷歌拥有一个非常广泛的全球基础设施,拥有顶级的云服务器。例如,如果您使用的是每月2欧元的固定位置托管服务提供商,那么您就不能期待与google一样的处理能力和网络能力。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62424267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档