Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to post multipart/form-data from Angular to Nodejs Multer?

From Angular I want to upload a image as Blob data to nodeJS server. The server uses multer in the backend. The image file is generated by canvas render. I am getting the following error from the server:

Error: Multipart: Boundary not found status:500

The following is my code. Please help me to find out the issue.

Angular:

// blob:Blob;   ->  it has valid image data.
var formData: FormData = new FormData();
formData.append('banner', blob, "my-file.png")

this.http.post(url,
    formData, { headers: new Headers({ 'Content-Type': 'multipart/form-data' }) })
    .toPromise()
    .then(res => {
      console.log(res);
      return res.json();
    })
    .catch(this.handleError);

nodejs:

router.post('/upload-banner-image', bannerImageUpload.single('banner'), watchfaceController.uploadWatchfaceBannerImage);
like image 580
PARAMANANDA PRADHAN Avatar asked May 09 '17 13:05

PARAMANANDA PRADHAN


Video Answer


2 Answers

Add Content-Type': 'file' in header and it should work

like image 139
Kamalakar Avatar answered Oct 14 '22 12:10

Kamalakar


Remove your 'Content-Type': 'multipart/form-data' header and it should work.

I got the same error, this is due to the missing boundary=.. just after multipart/form-data like the following working request: enter image description here

When you remove your header, browsers will add it automatically with the boundary=.. and it works.

like image 36
bertrandg Avatar answered Oct 14 '22 12:10

bertrandg