9

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);

3 Answers 3

23

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This will work with any modern browser? or is this part of the Angular htttpClient?
angular automatically sends the contentType: multipart/form-data ?
Yes, it works, it obtained automatically.
1

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

Comments

0

When you are using interceptor in angular you can make use of this code snippet

UploadFileService.ts

 uploadFile(file: File) {
    const formData: FormData = new FormData();
    formData.append('file', file);

    return this.http.post<any>(`${this.api}/${this.apiName}`, formData);
  }

http.interceptor.ts

export class APIInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler) {
    let authReq;
    if (request.method === 'POST' && request.body instanceof FormData) {
      authReq = request.clone({
        setHeaders: {
          Authorization: `Bearer ${this.auth.getToken()}`,
        },
      });
    } else {
      authReq = request.clone({
        setHeaders: {
          'Content-Type': 'application/json; charset=utf-8',
          Accept: 'application/json',
          Authorization: `Bearer ${this.auth.getToken()}`,
        },
      });
    }
    return next.handle(authReq).pipe(catchError((err) => this.handleAuthError(err)));
  }
}

Comments

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.