angular - 如何使用 nativescript-background-http 获取总上传大小
问题描述
我已经使用 angular 和 antivescript 创建了文件上传。当我上传单个文件时,currentBytes 和 totalBytes 工作正常 - 意思是:totalBytes 不会改变,currentBytes 会增加。多重上传的情况不同。选择多个文件时,看起来它开始上传块,总比特和当前比都不同。以这种方式做进度指示器是不可能的。任何想法如何让 currentBytes 的 totalBytes 一致以进行多次上传?
'完成'也被多次调用 - 有没有其他方法而不是增加局部变量,当它的值等于文件数量时,然后调用完成回调?
我将不胜感激任何帮助。
这是我的功能:
uploadFiles(images: ImageAsset[]): Observable<ImageUploadResponse> {
this.componentService.loadingIndicator.showMessage({ message: 'Uploading Images', showProgressIndicator: true });
return new Observable<ImageUploadResponse>(observer => {
const session = backgroundHttp.session('image-upload');
const uploadParams = [];
if (images.length === 0) {
observer.next({ imageIds: [] });
observer.complete();
return;
}
let imagesUploadCompleted = 0;
images.forEach(async image => {
// const imageSource = await ImageSource.fromAsset(image);
let localPath: any;
if (platformModule.isAndroid) {
localPath = image.android;
} /* else {
// selected_item.ios for iOS is PHAsset and not path - so we are creating own path
const folder = fileSystemModule.knownFolders.documents();
const path = fileSystemModule.path.join(folder.path, uuid.v1() + '.jpeg');
imageSource.saveToFile(path, 'jpeg');
localPath = path;
} */
uploadParams.push({ name: 'files', filename: localPath, mimeType: 'image/jpeg' });
const request = {
url: `${environment.apiUrl}/upload`,
method: 'POST',
headers: { 'Content-Type': 'application/octet-stream' },
description: 'Uploading images'
};
const task = session.multipartUpload(uploadParams, request);
task.on('progress', (e) => {
if (e.currentBytes) {
// console.log('progress ->', e.currentBytes);
this.ngZone.run(() => {
this.componentService.loadingIndicator.showProgress({ maxValue: e.totalBytes, value: e.currentBytes });
});
}
});
task.on('error', (e) => {
this.ngZone.run(() => {
observer.error(e.error);
observer.complete();
});
});
task.on('complete', (e: any) => {
imagesUploadCompleted++;
if (imagesUploadCompleted === images.length) {
this.ngZone.run(() => {
observer.next(JSON.parse(e.response.getBodyAsString()));
observer.complete();
});
}
});
// const file = fileSystemModule.File.fromPath(image['_android']);
// paths.push(imageSource.toBase64String('jpeg', 60));*/
});
});
}
解决方案
问题是,正如 Manoj 所说,请求是在循环中创建的。
推荐阅读
- apache-flink - 维基百科编辑事件中标志的含义
- r - 计算多列总数的百分比
- android - 如何在 Android N 上为 NativeActivity 动态加载本机共享库(.so)?
- python - 从有向图创建无向图
- c# - 无法将 System.string 转换为 System.Byte[]
- reactjs - 是什么让 React 库需要 preact-compat?
- android - 跨平台 IAP
- azure - Azure 中是否有任何用于高速文件共享的选项?
- indexing - 索引未从 uicollectionviewcell 传输到视图控制器
- sql-server - 使用 SQL 分发记录