【问题标题】:How to get total upload size with nativescript-background-http如何使用 nativescript-background-http 获取总上传大小
【发布时间】:2020-04-13 08:33:14
【问题描述】:

我已经使用 angular 和 antivescript 创建了文件上传。当我上传单个文件时,currentBytes 和 totalBytes 工作正常 - 意思是:totalBytes 不会改变,currentBytes 会增加。 多重上传的情况不同。选择多个文件时,看起来它开始上传块和CONTUBETES和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));*/
            });
        });
    }

【问题讨论】:

  • 你能展示一些代码吗,你是如何处理多个文件和事件的?
  • 我已经用功能更新了问题
  • 每次都不同,因为你是在循环中编写它,每次调用它时,回调可能针对你创建的不同文件/任务。您可能需要检查您的 API 服务器,看看它们是否支持一次上传多个文件,如果支持,那么您可以创建一个任务来上传所有文件,从而轻松解决您的问题。
  • 谢谢,我怎么会错过它在循环中:)))))它解决了一个问题。

标签: angular file-upload progress-bar nativescript nativescript-background-http


【解决方案1】:

问题是,正如 Manoj 所说,请求是在循环中创建的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    • 2018-09-24
    相关资源
    最近更新 更多