Angular2:上传大文件时重新加载表

时间:2017-02-09 21:34:45

标签: angular angular2-http

我有一个页面,其中列出了我上传的文件(从服务器获取此文件) 和上传按钮,一旦我点击上传,它从服务器发出GET请求给我上传状态(服务器可以提供反馈) 问题是当我上传大文件时,它不会进入“读取状态”功能。

以下是我正在使用的代码部分:

upload() {
    console.log("Clicked upload");
    if(this.file.size > 0) {
        let formData:FormData = new FormData();
        formData.append('csv_file', this.file, this.file.name);
        console.log("uploading file:");
        var FileName = this.file.name;
        this.startTimer();
        this._httpRequest.makeFileUpload('csv_upload', formData, FileName)
        .subscribe(MyTests => {
          this.noFile = true;
          this.resetFileName();
          this.ReadData(); <----------- not running this
        },
        () => { 
          console.log("Error sending file") ;
          this.errorLoading = true;
          }
        );
    }
  }

当然我无法在上传开始之前调用this.ReadData()(读取数据是GET HTTP请求) 有没有办法克服它?

enter image description here

1 个答案:

答案 0 :(得分:0)

通过将代码顺序更改为以下

来解决此问题

 upload() {
    console.log("Clicked upload");
    if(this.file.size > 0) {
        let formData:FormData = new FormData();
        formData.append('csv_file', this.file, this.file.name);
        var FileName = this.file.name;
        this._httpRequest.makeFileUpload('csv_upload', formData, FileName)
        .subscribe(MyTests => {
          this.isLoading=false;
          this.noFile = true;
          this.resetFileName();
         },
           () => { 
          this.errorLoading = true;
          this.isLoading=false;
         }
        );
         this.startTimer();
         this.ReadData();
    }
  }