Problem sending file with Angular Form-Data

Asked

Viewed 307 times

-1

In my project, I have to send a file to my backend, but when I get to the back it doesn’t take my parameters, it’s probably a problem in my service.

follow my ts and service.

TS:

 sendMailing() {

    this.dashboardService.sendMailing(this.mailing, this.infoUser.Id).subscribe(res => {
      console.log(res)
    });
    
  }

Service:

sendMailing(file, id) {
    var parms = {"mailing": file, "userId": id};
    let headers = new Headers({ 'Content-Type': 'multipart/form-data' });
    let options = new RequestOptions({ headers: headers });

    return this.http
    .post(environment.fastzapUrl + "v1/user/mailing", parms, options)
    .map((res: Response) => res.json())
  }

  • Hello, add more details about the error. I recommend to reading

  • When it arrives on the back, have two variables waiting for the parameters to run, but it passes straight, then I treated to return an error msg

  • What is the status code returned? What is the error message?

  • the message was set by me, returns a string with a phrase pro user, not giving error in the process, the only problem is that it goes through the variables and does not save the parameters then it falls in the condition I did and returns the message I set pro user

1 answer

1


I believe the error is that you are setting content-type as form-data, but you are sending a json. Try this:

sendMailing(file, id) {
    //var parms = {"mailing": file, "userId": id};
    const formData = new FormData();
    formData.append("mailing", file);
    formData.append("userId", id);
    let headers = new Headers({ 'Content-Type': 'multipart/form-data' });
    let options = new RequestOptions({ headers: headers });

    return this.http
         .post(environment.fastzapUrl + "v1/user/mailing", formData, options)
         .map((res: Response) => res.json());
  }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.