So In this post, we going to create an ASP CORE Web API, where I need to allow a Mobile App client to upload a list of images and save them to the server.Basically, client Sends multiple files using ASP.net Core web API.

I am creating a ASP CORE Web API where a user can post, something like a gallery image. I am trying to load an image and text into the database, for that. and for achieving that task I need to create an API for the front developer. and this endpoint accepts multiple images.

Model Class

 public class GalleryModel
    {
        [Required]
        public int UserId { get; set; }

        [Required]
        public List<IFormFile> Images { get; set; }
    }

So we are going to create an API for Upload Multiple File. 

[Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly DbContext _context;
        private IHostingEnvironment _hostingEnvironment;
        public UserController(DbContext context, IHostingEnvironment environment)
        {
            _context = context;
            _hostingEnvironment = environment ?? throw new ArgumentNullException(nameof(environment));
        }

        // Post: api/User/UpdateGallery
        [HttpPost]
        public async Task<IActionResult> UpdateGallery([FromForm] GalleryModel galleryModel)
        {
            Dictionary<string, string> resp = new Dictionary<string, string>();
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            try
            {
                //getting user from the database
                if (galleryModel.Images != null)
                {
                    var path = Path.Combine(_hostingEnvironment.WebRootPath, "galleryimage/");
                    //checking if "galleryimage" folder exist or not exist then create it
                    if ((!Directory.Exists(path)))
                    {
                        Directory.CreateDirectory(path);
                    }
                    foreach (var file in galleryModel.Images)
                    {
                        //getting file name and combine with path and save it to server folder
                        string filename = file.FileName;
                        using (var fileStream = new FileStream(Path.Combine(path, filename), FileMode.Create))
                        {
                            await file.CopyToAsync(fileStream);
                        }
                        //here you can write you logic for saving it in the database
                        
                        
                        
                    }
                }

            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
//return api with response
                        resp.Add("status ", "success");
            return Ok(resp);
        }
    }

Let’s Check with postman, open postman and try upload multiple image

Uploading multiple image

you can in the model , we are receiving multiple images.

Getting Multiple image
*Thank you so much if you have a question please comment

The post [Simple Way]-ASP.NET Core Upload Multiple File Web API model appeared first on Software Development | Programming Tutorials.



Read More Articles