【发布时间】:2021-05-26 16:08:08
【问题描述】:
我正在尝试按队列上传图片。目前,我需要按五次上传图片
例如,如果我有 10 张图片,我需要上传 5 张图片,当上传这 5 张图片时,我需要再次请求剩余的 5 张图片。
经过大量搜索和测试大量 RXJS 运算符/函数后,我可以通过(concat - RXJS 函数)和(scan 和 last - RXJS 运算符)找到这个解决方案:
在 index.html 中
// added chunk function into array protptype
Array.prototype.chunk = function (chunkSize) {
var R = [];
for (var i = 0; i < this.length; i += chunkSize) {
R.push(this.slice(i, i + chunkSize));
}
return R;
}
在服务中
uploadImages(images: File[]) {
const requestsList = images.map(file => {
return this._baseAPI.filesManagement.uploadFile({ file, 'image' })
});
// custom function: added manualy in index.html
const requestChunks = requestsList.chunk(5);
const requestsByChunks = requestChunks.map(reqs => forkJoin(reqs));
return concat(...requestsByChunks).pipe(
// transforming and collecting all of the responses
scan((acc, response: {folder: string; image: string}[]) => {
response.forEach(itemResponse => {
if (!acc[itemResponse.folder]) { acc[itemResponse.folder] = []; }
acc[itemResponse.folder].push(itemResponse.image);
});
return acc;
},
{} as { [key: string]: string }),
// getting the callected response only once at the end
last()
);
}
使用示例
this.uploadImages(/* Array of files*/).subscribe(response => {console.log(response); })
我想知道对于这些情况是否有任何其他解决方案或最佳实践。也许我错过了一些东西,因为我不确定这是最好的解决方案?
还有:这里是当前逻辑的模拟:https://stackblitz.com/edit/rxjs-kcnrgn?file=index.ts
感谢大家的宝贵时间。
【问题讨论】: