0

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

8
  • Did you double check that your webserver allows the PUT method? Commented Jan 2, 2018 at 10:28
  • yes, when I use the PUT method with POSTMAN everything works fine. Commented Jan 2, 2018 at 10:34
  • Which webserver are you using? Commented Jan 2, 2018 at 10:48
  • I'm using spring boot back-end with embedded tomcat. Commented Jan 2, 2018 at 10:57
  • Refer this post stackoverflow.com/questions/43830945/… Commented Jan 2, 2018 at 11:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.