Angular上传进度报告不正确
创始人
2024-10-29 10:00:43
0

要解决Angular上传进度报告不正确的问题,可以尝试以下方法:

  1. 使用HttpClient模块的observe选项:

    import { HttpClient, HttpEventType } from '@angular/common/http';
    
    constructor(private http: HttpClient) { }
    
    uploadFile(file: File) {
      const formData = new FormData();
      formData.append('file', file);
    
      this.http.post('upload-url', formData, {
        reportProgress: true,
        observe: 'events'
      }).subscribe(event => {
        if (event.type === HttpEventType.UploadProgress) {
          const progress = Math.round(100 * event.loaded / event.total);
          console.log(`Upload progress: ${progress}%`);
        } else if (event.type === HttpEventType.Response) {
          console.log('Upload complete');
        }
      });
    }
    
  2. 使用XMLHttpRequest对象:

    uploadFile(file: File) {
      const formData = new FormData();
      formData.append('file', file);
    
      const xhr = new XMLHttpRequest();
      xhr.open('POST', 'upload-url', true);
    
      xhr.upload.addEventListener('progress', event => {
        if (event.lengthComputable) {
          const progress = Math.round(100 * event.loaded / event.total);
          console.log(`Upload progress: ${progress}%`);
        }
      });
    
      xhr.onreadystatechange = () => {
        if (xhr.readyState === XMLHttpRequest.DONE) {
          console.log('Upload complete');
        }
      };
    
      xhr.send(formData);
    }
    
  3. 使用第三方库ngx-uploadx:

    import { UploadxOptions, UploadState, UploadxService } from 'ngx-uploadx';
    
    constructor(private uploadService: UploadxService) { }
    
    uploadFile(file: File) {
      const options: UploadxOptions = {
        url: 'upload-url',
        concurrency: 1,
        autoUpload: true,
        allowedTypes: '.png, .jpg',
        headers: {
          Authorization: 'Bearer ' + localStorage.getItem('token')
        },
        onProgress: (state: UploadState) => {
          const progress = Math.round(100 * state.loaded / state.total);
          console.log(`Upload progress: ${progress}%`);
        },
        onCompleteItem: (item, response, status, headers) => {
          console.log('Upload complete');
        }
      };
    
      this.uploadService.handleFiles([file], options);
    }
    

以上方法中,第一种方法使用了Angular的HttpClient模块来发送HTTP请求,并使用observe选项和HttpEventType来获取上传进度和完成状态。第二种方法使用了原生的XMLHttpRequest对象来发送请求,并监听progress事件和readystatechange事件来获得上传进度和完成状态。第三种方法使用了第三方库ngx-uploadx,它提供了更多的功能和配置选项来处理文件上传,并通过回调函数提供了上传进度和完成状态的通知。根据需求选择适合的方法来解决问题。

相关内容

热门资讯

透视存在!fishpoker透... 透视存在!fishpoker透视,拱趴大菠萝辅助神器,技巧教程(有挂黑科技);暗藏猫腻,小编详细说明...
透视免费!wepoker私人局... 透视免费!wepoker私人局俱乐部,佛手在线十三道辅助器,详细教程(有挂介绍);1、操作简单,无需...
透视存在!德州局透视脚本下载安... 透视存在!德州局透视脚本下载安装最新版本,雀友会广东潮汕麻雀开挂,安装教程(有挂方法)一、雀友会广东...
透视了解!wepoker透视底... 透视了解!wepoker透视底牌脚本,苹果手机闲逸辅助器,必胜教程(有挂规律)1、上手简单,内置详细...
透视代打!pokerworld... 透视代打!pokerworld破解版下载,财神十三张如何提高运气,安装教程(有挂解说)1、实时财神十...
透视科技!pokerworld... 透视科技!pokerworld辅助器,浙江宝宝游戏辅助器,2025新版技巧(有挂方法)浙江宝宝游戏辅...
透视透视!wepoker作弊辅... 透视透视!wepoker作弊辅助,乾坤互娱辅助,wpk教程(有挂解说);乾坤互娱辅助是一种具有地方特...
透视挂透视!hhpoker免费... 透视挂透视!hhpoker免费透视脚本,禅游游戏辅助脚本,透牌教程(有挂介绍)1、上手简单,内置详细...
透视教学!wepokerplu... 透视教学!wepokerplus透视脚本免费,小程序财神十三张辅助,可靠教程(有挂技巧);1、用户打...
透视ai代打!hhpoker有... 透视ai代打!hhpoker有没有辅助,情怀七喜游戏辅助,安装教程(有挂教程);1、进入到情怀七喜游...