Angular4 file upload PUT request fails

拥有回忆 提交于 2019-12-23 04:39:34

问题


Angular4 file upload to PUT method fails but for POST it works fine. Any one having idea what's going on here??

HTML

<input #fileSelect type="file" class="form-control" (change)="onFileUpload($event)"  accept=".jpg, .png"/>

COMPONENT

   imageToUpload: any;
    @ViewChild('fileSelect') fileSelectInput: ElementRef;

    onFileUpload(event) {
        this.imageToUpload = event.srcElement.files;
      }

    uploadImage() {
      let formData: FormData = new FormData();
      formData.append('file', this.imageToUpload[0]);

    this.http.post(this.apiUrl, formData, { responseType: 'text' }).subscribe((imageId) => {
          // response
        });
    }

// fails with 400 bad request
    updateImage(formData: FormData, imageId: string) {    
        this.http.put(`${this.apiUrl}/${imageId}`, formData).subscribe((response) => {
          // response
        });
      }

For POST no issues, files are uploading perfectly. But for PUT getting 400 Bad Request error. Any idea what's going on? I'm scratching my head from last 2 days. Any help would be really appreciable.

Thanks

来源:https://stackoverflow.com/questions/48059121/angular4-file-upload-put-request-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!