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
you can in the model , we are receiving multiple images.
*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
- Write a value which contain comma to a CSV file in c#?
- Reading CSV File with cells containing commas c#
- Split CSV with columns may contain ‘,’ Comma C#
- [Simple Way]-Cascading DropDownList in Asp.Net Mvc Using Jquery Ajax
- [Simple Way]-How to get data from database using JQuery Ajax in asp net MVC
- [Simple Way]-ASP.NET Core Upload Multiple File Web API model
- [Simple Way]- Image Upload in .NET Core Web API
- [Easy Way]-Receive File and other form data together in ASP.NET Core Web API
- Replace image in word document using C#
- How to add new rows to an existing word document table in C#
- How can i post both file and text data to web api using asp net core http client?
- ASP .NET Core Web API - Getting and extracting a .zip file from upload controller, using IFormFile
- ASP.NET Core Web API - How to upload multiple files to specific directory
- How to make all model properites nullable in asp net core api
- .NET Web API Blueimp multiple file upload error "Unexpected end of MIME multipart stream. MIME multipart message is not complete."
- When adding multiple dbcontext In Web API . Net core all DBSet returns null
- Unable to upload file size larger than 200 MB in ASP.NET Core Web API
- JsonConvert.DeserializeObject File into a model class web api .net core
- Asp.net Core Web API - Downloading multiple images from blob storage to zip file (via Axios get)
- Error : Connection Refused. Upload file to Asp Web Api
- Multiple File Upload in Web Api using jQuery Ajax
- How to do one to many relation with mongodb in Asp .net core web api
- Model binding stopped working in case of ASP.NET Core 3.1 Web API
- ASP.NET Core list model binding backward compatibility with legacy ASP.NET Web API
- Can't return multiple object on asp.net core web api
- ASP.NET Core Web API - implement multiple databases using Sybase and SQL Server
- Upload file in angular / web API
- ASP.NET Core Web API - How to store combination of attributes as raw data into a single column of another Model
- How to use JWT token to authorize user from react to asp net core web api. When to use autorization header bearer token?
- Add custom attribute to OpenAPI specification file and swagger in .net core web api
- Returning multiple rowsets from SQL Server using a stored procedure via an ASP.NET Core Web API
- Named semaphore not working on ASP .Net Core 5 Web API hosted on Azure App Service
- .Net core 5 web api send file in content header?
- How to handle encrypt and decrypt file in ASP.NET core web API
- How to read multiple connectionstrings in appsetting.json file in .net core api project
- Download PDF File from Shared Network Drive in .NET Core 3.1 Web Api
- .net core web api File randomly loads only part of the response
- Net Core REST API - Returning JSON with and without a model
- Running powershell 7 script in asp net core web appllication
- Posting a file from ASP.NET MVC 4.6 application to ASP.NET Core Web API
- Local function overloading don't work: "local variable already defined in this scope"
- Gridview issue Object cannot be cast from DBNull to other types
- Retrieving IIS 6.0 certificate information in .NET
- How to refresh nested control property in UserControl
- Private appointments in Exchange room's account listed as non-private
- Search for word in ListBox written on InputBox (C# VS)
- Get Principal Context object using LDAP path
- How to sort result based on hit score in NEST elastic search with Highlights
- How to simplify conditional Lambda using Switch
- HotChocolate passing different class for Interface for different query in DI
- How to make optional Model Class Properties in Asp.Net Web Api
- Getting permission for accessing an existing file in setup created using Inno Setup
- How to convert HTML to PDF and add headers/footers
- how can I create Class object using Reflection in c#?
- Can't do anything inside my custom event
- Performance penalty when using fluent interface
- How to handle resource loading in a control that is packed into a nuget?
- Displaying two dimensional array in a method
- Printing the contents of ScrollViewer in C# UWP
- why i get this error when i do allow paging True The data source does not support server-side data paging