In this blog, we learn about upload image with other parameter & upload image via Postman,
Step 1
Open Visual Studio 2019 Or Visual Studio 2022 & Click on create project select ASP.net core web API template
Step 2
Add line in Program.cs file after "app.UseStaticFiles();"
app.UseStaticFiles();
Step 3
Create "wwwroot" folder under Project solution
Step 4
Create new FileUploadAPI class & Customer class under project solution
FileUploadAPI.cs
namespace UploadImageWithData8777
{
public class FileUploadAPI
{
public int ImgID { get; set; }
public string? Customers { get; set; }
public IFormFile? files { get; set; }
public string ImgName { get; set; }
}
public class common
{
public FileUploadAPI _fileAPI { get; set; }
public Customer _Customer { get; set; }
public List<Customer> LstCustomer { get; set; }
}
}
Customer.cs
namespace UploadImageWithData8777
{
public class Customer
{
public int CustID { get; set; }
public string? CustName { get; set; }
public string? CustomerType { get; set; }
}
}
Step 5
Add ImageUploadController Under controller folder
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace UploadImageWithData8777.Controllers {
[Route("api/[controller]")]
[ApiController]
public class ImageUploadController: ControllerBase {
public static IWebHostEnvironment _environment;
public ImageUploadController(IWebHostEnvironment environment) {
_environment = environment;
}
[HttpPost]
public Task < common > Post([FromForm] FileUploadAPI objFile) {
common obj = new common();
obj.LstCustomer = new List < Customer > ();
obj._fileAPI = new FileUploadAPI();
try {
List < Customer > list = JsonConvert.DeserializeObject < List < Customer >> (objFile.Customers);
obj.LstCustomer = list;
obj._fileAPI.ImgID = objFile.ImgID;
obj._fileAPI.ImgName = "\\Upload\\" + objFile.files.FileName;
if (objFile.files.Length > 0) {
if (!Directory.Exists(_environment.WebRootPath + "\\Upload")) {
Directory.CreateDirectory(_environment.WebRootPath + "\\Upload\\");
}
using(FileStream filestream = System.IO.File.Create(_environment.WebRootPath + "\\Upload\\" + objFile.files.FileName)) {
objFile.files.CopyTo(filestream);
filestream.Flush();
// return "\\Upload\\" + objFile.files.FileName;
}
}
} catch (Exception ex) {
throw;
}
return Task.FromResult(obj);
}
}
}
Step 6
Change configuration in launchSettings,json file,
{
"profiles": {
"UploadImageWithData8777": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/ImageUpload",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:7273;http://localhost:5273"
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/ImageUpload",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"WSL": {
"commandName": "WSL2",
"launchBrowser": true,
"launchUrl": "https://localhost:7273/swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_URLS": "https://localhost:7273;http://localhost:5273"
},
"distributionName": ""
}
},
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:59880",
"sslPort": 44390
}
}
}
Step 7
Run Application & Make Request From PostMan tool.
Enter API Url in postman : https://localhost:44390/api/ImageUpload & Select methodtype=POST go to body Select form-data Enter parameter details & select image file & select type file in key
Sample form-Data
KEY |
VALUE |
files |
Tractor.jpg |
customers |
KEY
[
{
"custID": 1,
"custName": "Sai",
"customerType": "Reguler"
}
,
{
"custID": 2,
"custName": "Sharayu",
"customerType": "Reguler"
}
] |
ImgID |
1001 |
ImgName |
Tractor |
OUTPUT
{
"_fileAPI": {
"imgID": 1001,
"customers": null,
"files": null,
"imgName": "\\Upload\\Tractor.jpg"
},
"_Customer": null,
"lstCustomer": [
{
"custID": 1,
"custName": "Sai",
"customerType": "Reguler"
},
{
"custID": 2,
"custName": "Sharayu",
"customerType": "Reguler"
}
]
}
Thank you for Reading.