首页 > 解决方案 > Angular 7 和 FormData

问题描述

我有角度和formData的问题。当我尝试使用 formData.get('name') 时,我收到消息:“对象 [object formData] 没有方法获取”

有人知道为什么会这样

我现在不知道改变这个的另一种方法

page.html

  <ion-item>
    <ion-input type="file" accept="image/*" (change)="onFileSelected($event)" id="profilePhotoFileUpload"></ion-input>  
  </ion-item>

页面.ts

onFileSelected(event){
  this.selectedFile = <File>event.target.files[0];
  this.onUpload(this.selectedFile)
}
  onUpload(selectedFile: File){
    //let fd: FormData = new FormData();
    var fd = new FormData();
    fd.append('imageFile', this.selectedFile, this.selectedFile.name);

    const httpOptions = {
      headers: new HttpHeaders({
        'X-Parse-Application-Id': 'JrFPfCeUABVCCDcmP2B2vaaghMIStiFmY9mz6Gv5',
        'X-Parse-REST-API-Key': 'VgSHmRKTBamNL5VKUS2qYv7rL5awKprx4iigwDaf'
       })
    };
    this.http.post(`${this.serverURL}files/myImage.png`,fd.get('imageFile'),httpOptions).subscribe(
      res =>{
        const object = {
          name: "TestUpload",
          file:{
            url: res["url"],
            name: res["name"],
            __type:"File"
          }
        }

        this.http.post(`${this.serverURL}/classes/User`,object,httpOptions).subscribe(res1=>{
          alert(JSON.stringify(res1));
        });
      }
    );
  }  

“对象 [object formData] 没有获取方法”

标签: javascriptangularionic-framework

解决方案


推荐阅读