Sending multiple images and data in a single angular observable

36 views Asked by At

I am attempting to send multiple images and data within the same observable. I conducted some research, but all I found was information on how to send a single file. My API is prepared to accept multiple files and data; I have tested it with Postman.

formData.set("IdUsuario", IdUsuario);
formData.set("IdSucursal", IdSucursal);
formData.set("IdMarca", IdMarca);
formData.set("Estatus", Estatus);
formData.set("Nombre", Nombre);
formData.set("SKU", SKU);
formData.set("Tipo", Tipo);
formData.set("Costo", Costo);
formData.set("Precio", Precio);
formData.set("Descripcion", Descripcion);    
if(Array.isArray(params.images) && params.images.length !== 0){
  params.images.forEach((image: any) => {
   formData.append("Imagenes", image);
  });
}

return this.http.post(`${environment.server}producto/insertar`, formData);

My API works very well. I found that it's important to send the data and images with formData.

1

There are 1 answers

0
Francisco José Muñoz García On

Resolved! It was necessary to use [] in my 'Imagenes' attribute, which allowed me to send the images as an array.

if(Array.isArray(params.images) && params.images.length !== 0){
    params.images.forEach((image: any) => {
        formData.append("**Imagenes[]**", image);
    });
}

enter image description here