【问题标题】:Not able to append data to the FormData无法将数据附加到 FormData
【发布时间】:2018-06-22 16:32:10
【问题描述】:

我正在尝试将文件作为 multipart /form-data 上传到服务器。为此,我创建了 FormData 的对象并将文件及其名称附加到其中。不幸的是,追加没有发生并且没有显示任何错误。请帮我解决这个问题。以下是我尝试过的代码。

HTML

  <input style="font-size:15px;" name="file" type="file" (change)="fileChangeEvent($event)" placeholder="Upload a file..."/>

      <button [disabled]="!this.fileCollection.length>0" type="button" class="btn btn-primary btn-s" (click)="uploadFile()" style="position:relative; top: 12px;"> 
          Upload
      </button>

component.ts

fileChangeEvent(fileInput: any): void {
    this.filesToUpload = fileInput.target.files;
    this.uppendFileCollections(this.filesToUpload);
  }

  uppendFileCollections(files): void {
    for (var index = 0; index < files.length; index++) {
      {
        this.fileCollection.push(<TemplateForm>
          {
            fileData: files[index],
            fileName: files[index].name
          }
        );
      }
    }

    this.fileUploaderTitle = this.fileCollection.length + ' file(s) selected.';
  }

  uploadFile() {
    let file: File = this.fileCollection[0]['fileData'];//passing only one file now.

    this.homeService.saveFile(file)
      .then((response) => {
        if (response.message == 'success') {
          this.filesToUpload = [];
          this.fileCollection = [];
          this.fileUploaderTitle = 'Choose Files';
          this.showSnackBar();
          this.isLoading = false;
        }
        else {
          console.log('Error on upload:' + response.error);
        }
        this.router.navigate(['/result'])
      })
  }

Service.ts

saveFile(file: File): Promise<any> {
        let uri = `${this.serverApi}/fileUpload`;
        let playload: FormData = new FormData();
        playload.append('file', file,file.name);
        let headers = new Headers();
        headers.append('Content-Type', 'multipart/form-data');
        headers.append('Accept', 'application/json');
        let options = new RequestOptions({ headers: headers });
        return this.http.post(uri, playload, options)
          .toPromise()
          .then((res) => {
            let temp = res.json();
            return { 'statusCode': 200, 'message': 'success', 'result': temp, 'error': {} };
          }
          )
          .catch((error) => {
            return { 'statusCode': 200, 'message': 'failed', 'result': {}, 'error': error };
          }
          )
      }

【问题讨论】:

  • 通过调试检查您的服务,看看您是否收到了该名称的对象
  • @WiselyDCruizer 从这里本身我认为附加不起作用。从服务器端,它接收到的表单数据对象没有数据。
  • 贴出你的html代码和组件代码
  • @WiselyDCruizer 我已经更新了我的问题。

标签: angularjs typescript multipartform-data


【解决方案1】:

使用这个简单的格式:

saveFile(data)
{
 let uri = `${this.serverApi}/fileUpload`;
 const formData: FormData = new FormData();
 formData.append('file',file,file.name);
 return this.http
  .post(uri, formData)
  .map(() => { return true; })
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    • 2015-12-23
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 2015-11-12
    相关资源
    最近更新 更多