[英]How to get total upload size with nativescript-background-http
I have created file upload using angular and antivescript.我使用 angular 和 antivescript 创建了文件上传。 When i am uploading single file currentBytes and totalBytes works ok - meaning: totalBytes does not change and currentBytes increasing.
当我上传单个文件时,currentBytes 和 totalBytes 工作正常 - 意思是:totalBytes 不会改变,而 currentBytes 会增加。 Situation is different with multi upload.
多次上传的情况不同。 When selecting multiple files looks like it starts to uploads chunks and totalBytes and currentBytes different every callback.
选择多个文件时,它开始上传块,并且每个回调的 totalBytes 和 currentBytes 都不同。 It is impossible to do progress indicator this way.
这样做进度指示器是不可能的。 Any ideas how to get totalBytes of currentBytes consistant for multi-upload?
任何想法如何使 currentBytes 的 totalBytes 与多次上传一致?
Also 'complete' is called multiple times - is there any other way rather then increment local variable and when its value equal to amount of files then call complete callback? “完成”也被多次调用——有没有其他方法而不是增加局部变量,当它的值等于文件数量时调用完成回调?
I would appreciate any help.我将不胜感激任何帮助。
Here is my function:这是我的 function:
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));*/
});
});
}
Problem was, as Manoj said, requests were created in the loop.问题是,正如 Manoj 所说,请求是在循环中创建的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.