How to use angular2 http API for tracking upload/download progress

前端 未结 2 1904
梦如初夏
梦如初夏 2020-12-07 23:32

Tho there are many adhoc libraries supporting upload/download progress in angular2, I do not know how to do use native angular2 http api to show progress while doing upload/

2条回答
  •  孤街浪徒
    2020-12-07 23:57

    I would suggest using the native JavaScript XHR wrapped as an Observable, it's fairly easy to create on your own:

    upload(file: File): Observable {
    
        let fd: FormData = new FormData();
    
        fd.append("file", file);
    
        let xhr = new XMLHttpRequest;
    
        return Observable.create(observer => {
    
            xhr.addEventListener("progress", (progress) => {
    
                let percentCompleted;
    
                // Checks if we can really track the progress
                if (progress.lengthComputable) {
    
                    // progress.loaded is a number between 0 and 1, so we'll multiple it by 100
                    percentCompleted = Math.round(progress.loaded / progress.total * 100);
    
                    if (percentCompleted < 1) {
                        observer.next(0);
                    } else {
                        // Emit the progress percentage
                        observer.next(percentCompleted);
                    }
                }
            });
    
            xhr.addEventListener("load", (e) => {
    
                if (e.target['status'] !== 200) observer.error(e.target['responseText']);
    
                else observer.complete(e.target['responseText']);
            });
    
            xhr.addEventListener("error", (err) => {
    
                console.log('upload error', err);
    
                observer.error('Upload error');
            });
    
            xhr.addEventListener("abort", (abort) => {
    
                console.log('upload abort', abort);
    
                observer.error('Transfer aborted by the user');
            });
    
            xhr.open('POST', 'http://some-dummy-url.com/v1/media/files');
    
            // Add any headers if necessary
            xhr.setRequestHeader("Authorization", `Bearer rqrwrewrqe`);
    
            // Send off the file
            xhr.send(fd);
    
            // This function will get executed once the subscription
            // has been unsubscribed
            return () => xhr.abort()
        });
    }
    

    And this is how one would use it:

    // file is an instance of File that you need to retrieve from input[type="file"] element
    const uploadSubscription = this.upload(file).subscribe(progress => {
        if (typeof progress === Number) {
            console.log("upload progress:", progress);
        }
    });
    
    // To abort the upload
    // we should check whether the subscription is still active
    if (uploadSubscription) uploadSubscription.unsubscribe();
    

提交回复
热议问题