1

I was searching for a solution in the past day about this but never found a solution...

I am sending multiple images from angular using from data and if i check the network for the api call the files are sended but when i check the parameters on the api method it shows count=0

Angular Code

createProduct(product: any, images: File[]) {
// return this.http.post(this.createProductUrl, product).pipe(
  // switchMap((resp: Product) => {

    const colorId = '2';
    const productId = '3';
    const image = new FormData();

    for (let index = 0; index < images.length; index++) {
      image.append('files', images[index]);
    }


    return this.http.post(`${this.createImagesForProduct}/${productId}/color/${colorId}`, image)
    .pipe(map(
      resp => {
        return console.log(resp);
      }
    ));

Back End Method

Google DevTools Network Tab

please i need your help

2
  • In your controller action, is Request.Form.Files empty as well? Commented May 15, 2020 at 9:47
  • okk, i tried Request.Form.Files and its working but is there a way that i can achive that using fromform? Commented May 15, 2020 at 10:50

1 Answer 1

3

Convert your request body into model

public class Model{
  public int productId{get;set;}
  public int colorId{get;set;}
  public List<IFormFile> files{get;set;}
}

in request write

public async Task<IActionResult> CreateImage(Model param){}

in angular put all elements into formdata

    const image = new FormData();
    image.append('productId',`${productId}`);
    image.append('colorId',`${colorId}`);
    for (let index = 0; index < images.length; index++) {
        image.append('files', images[index]);
    }
    return this.http.post(`${this.createImagesForProduct}`, image)
        .pipe(map(
          resp => {
            return console.log(resp);
          }
        ));
Sign up to request clarification or add additional context in comments.

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.