首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JS分块文件上传承诺

JS分块文件上传承诺
EN

Stack Overflow用户
提问于 2018-02-28 10:47:21
回答 2查看 3.6K关注 0票数 0

我在建一个块文件上传器。我明白承诺是最好的处理结果的方法,但我仍然不能把我应该如何从外部捕获承诺的想法缠绕起来。

caller.js

代码语言:javascript
复制
let fileUploader = new ChunkedUpload()
    fileUploader.setFile(file)
    fileUploader.upload().then(result => {
       // Here I'd like to catch the current result of the file
       // If it's not uploaded, I need the percentage and continue 
       // If it is I need the response from the API
       console.log(result)
    })

uploader.js

代码语言:javascript
复制
upload() {
    let totalChunks = Math.ceil(this._file.size/this._sliceSize)

    return this._chunk(0, 0, totalChunks)
}

_chunk(start, chunk, totalChunks) {
    let lastChunk = false
    let end = start + this._sliceSize

    if (this._file.size - end < 0) {
        end = this._file.size
        lastChunk = true
    }

    let slice = this._sliceFile(this._file, start, end)

    let formData = new FormData()
    formData.append('chunk', chunk)
    formData.append('chunks', totalChunks)
    formData.append('file', slice)

    return new Promise((resolve, reject) => {
        axios.post(this._url, formData).then(response => {
            if (lastChunk) {
                // This part is okay, it returns the result
                resolve({
                    uploaded: true,
                    url: response.data,
                    uploadPercentage: 100,
                    uploadSize: this.formatBytes(file.size)
                });

            } else {
                // Here's the issue. Next chunk upload is called, 
                // however I cannot track the process in caller.js
                resolve(this._chunk(
                    start += this._sliceSize,
                    chunk += 1,
                    totalChunks
                ));
            }
        })
    })
}

我接受任何评论和任何。也许我的方法是错的,请告诉我!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-02-28 10:54:10

承诺应该被用于一次性回调-成功或错误。

你想要的是“进步”信息。

这样你就应该:

  • 使用回调函数代替获取有关进度的详细信息;或
  • 听并发出事件;

但是,如果你真的想使用诺言而不是使用回调或事件,我建议:

返回包含详细信息的承诺和一个名为continue()的方法,该方法必须被调用,因此该过程可能会继续。

下面是我给你的密码:

caller.js

代码语言:javascript
复制
let fileUploader = new ChunkedUpload()
    fileUploader.setFile(file)
    var callback = result => {
       // Here I'd like to catch the current result of the file
       // If it's not uploaded, I need the percentage and continue 
       // If it is I need the response from the API
       console.log(result);
       if (result.uploaded) {
           console.log('DONE');
       } else {
           console.log('STILL UPLOADING...');
           result.continue()
               .then(callback);
       }
    }
    fileUploader.upload().then(callback);

uploader.js

代码语言:javascript
复制
upload() {
    let totalChunks = Math.ceil(this._file.size/this._sliceSize)

    return this._chunk(0, 0, totalChunks)
}

_chunk(start, chunk, totalChunks) {
    let lastChunk = false
    let end = start + this._sliceSize

    if (this._file.size - end < 0) {
        end = this._file.size
        lastChunk = true
    }

    let slice = this._sliceFile(this._file, start, end)

    let formData = new FormData()
    formData.append('chunk', chunk)
    formData.append('chunks', totalChunks)
    formData.append('file', slice)

    return new Promise((resolve, reject) => {
        axios.post(this._url, formData).then(response => {
            if (lastChunk) {
                // This part is okay, it returns the result
                resolve({
                    uploaded: true,
                    url: response.data,
                    uploadPercentage: 100,
                    uploadSize: this.formatBytes(file.size)
                });

            } else {
                // Here's the issue. Next chunk upload is called, 
                // however I cannot track the process in caller.js
                // you may include in the object below the metrics you need
                resolve({
                    uploaded: false,
                    continue: () => {
                      return this._chunk(
                          start += this._sliceSize,
                          chunk += 1,
                          totalChunks
                    }
                });
            }
        })
    })
}
票数 1
EN

Stack Overflow用户

发布于 2018-02-28 11:48:26

谢谢拉法!你的回答把我引向了各种事件。

显然,正如@Bergi所提到的那样,我陷入了承诺构造反模式,捕捉到一个拒绝将导致另一个变量定义。

谢谢大家!

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

https://stackoverflow.com/questions/49027701

复制
相关文章

相似问题

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