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,它提供了更多的功能和配置选项来处理文件上传,并通过回调函数提供了上传进度和完成状态的通知。根据需求选择适合的方法来解决问题。

相关内容

热门资讯

揭秘教程!wepork私人局有... 1、揭秘教程!wepork私人局有挂(wepoke模拟器)wePoke(一直真的有挂);详细教程。2...
wpk有外 挂!德州wpk有外... wpk有外 挂!德州wpk有外 挂(WPK)原来真的有挂-详细透视教程1、任何WPKai辅助神器的玩...
德州免费辅助神器app!德扑软... 自定义德州免费辅助神器app系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器...
wepoke ai辅助!德州a... WePoker透视辅助版本解析‌,wepoke ai辅助!德州ai软件购买,德扑之星操作(详细透视挂...
wepoke辅助!wepowe... wepoke辅助!wepower软件(透视辅助)WepOke(的确真的有挂);《WPK辅助透视》‌:...
wpk透视辅助!wpk俱乐部长... wpk透视辅助!wpk俱乐部长期盈利打法,wPk一直真的有挂,详细教程,(有挂辅助);1、全新机制【...
德州ai辅助有用!德扑之星可以... 德州ai辅助有用!德扑之星可以设置,智星德州好像真的有挂(详细有猫腻教程);德州ai辅助有用!德扑之...
wpk提高胜率!微扑克微乐辅助... wpk提高胜率!微扑克微乐辅助,aapoker俱乐部后台可以操作(详细辅助教程);(需添加指定薇75...
WePoKe透明挂!wepok... WePoKe透明挂!wepoke有辅助,wopoker手机辅助,wepoke教程(有挂脚本);精心打...
德州之星有外 挂!德扑之星是有... 您好,德州之星有外 挂这款游戏可以开挂的,确实是有挂的,需要了解加微【757446909】很多玩家在...