gpt4 book ai didi

javascript - JS 分块文件上传与 promise

转载 作者:行者123 更新时间:2023-12-03 02:24:06 25 4
gpt4 key购买 nike

我正在构建一个分块文件 uploader 。我知道 Promise 是处理结果的最佳方式,但仍然无法确定应该如何从外部获取 Promise。

caller.js

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

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
));
}
})
})
}

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

最佳答案

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

您想要的是“进度”信息。

这样你应该:

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

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

返回包含详细信息的 promise ,以及内部名为 continue() 的方法,必须调用该方法,以便流程可以继续。

下面我给你代码:

caller.js

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

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
}
});
}
})
})
}

关于javascript - JS 分块文件上传与 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49027701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com