"save image IFOrmFile to path in asp.net 5 C# web api" Code Answer's
You're definitely familiar with the best coding language C# that developers use to develop their projects and they get all their queries like "save image IFOrmFile to path in asp.net 5 C# web api" answered properly. Developers are finding an appropriate answer about save image IFOrmFile to path in asp.net 5 C# web api related to the C# coding language. By visiting this online portal developers get answers concerning C# codes question like save image IFOrmFile to path in asp.net 5 C# web api. Enter your desired code related query in the search bar and get every piece of information about C# code related question on save image IFOrmFile to path in asp.net 5 C# web api.
save image IFOrmFile to path in asp.net 5 C# web api
using Microsoft.AspNetCore.Hosting;
public class ImageController : Controller
{
private readonly IWebHostEnvironment _hostEnvironment;
public ImageController(..., IWebHostEnvironment hostEnvironment)
{
...
this._hostEnvironment = hostEnvironment;
}
...
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("ImageId,Title,ImageFile")] ImageModel imageModel)
{
if (ModelState.IsValid)
{
//Save image to wwwroot/image
string wwwRootPath = _hostEnvironment.WebRootPath;
string fileName = Path.GetFileNameWithoutExtension(imageModel.ImageFile.FileName);
string extension = Path.GetExtension(imageModel.ImageFile.FileName);
imageModel.ImageName=fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
string path = Path.Combine(wwwRootPath + "/Image/", fileName);
using (var fileStream = new FileStream(path,FileMode.Create))
{
await imageModel.ImageFile.CopyToAsync(fileStream);
}
//Insert record
_context.Add(imageModel);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(imageModel);
}
....
}
Source: www.codaffection.com
save image IFOrmFile to path in asp.net 5 C# web api
[Route("api/[controller]")]
[ApiController]
public class CarsController : ControllerBase
{
private readonly IHostingEnvironment _hostingEnv;
private readonly WebAPIDbContext _context;
public CarsController(WebAPIDbContext context, IHostingEnvironment hostingEnv)
{
_hostingEnv = hostingEnv;
_context = context;
}
[HttpPost]
public async Task<ActionResult> Post([FromForm] CarViewModel carVM)
{
if (carVM.Image != null)
{
var a = _hostingEnv.WebRootPath;
var fileName = Path.GetFileName(carVM.Image.FileName);
var filePath = Path.Combine(_hostingEnv.WebRootPath, "images\\Cars", fileName);
using (var fileSteam = new FileStream(filePath, FileMode.Create))
{
await carVM.Image.CopyToAsync(fileSteam);
}
Car car = new Car();
car.CarName = carVM.CarName;
car.ImagePath = filePath; //save the filePath to database ImagePath field.
_context.Add(car);
await _context.SaveChangesAsync();
return Ok();
}
else
{
return BadRequest();
}
}
}
Source: entityframeworkcore.com
All those coders who are working on the C# based application and are stuck on save image IFOrmFile to path in asp.net 5 C# web api can get a collection of related answers to their query. Programmers need to enter their query on save image IFOrmFile to path in asp.net 5 C# web api related to C# code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about save image IFOrmFile to path in asp.net 5 C# web api for the programmers working on C# code while coding their module. Coders are also allowed to rectify already present answers of save image IFOrmFile to path in asp.net 5 C# web api while working on the C# language code. Developers can add up suggestions if they deem fit any other answer relating to "save image IFOrmFile to path in asp.net 5 C# web api". Visit this developer's friendly online web community, CodeProZone, and get your queries like save image IFOrmFile to path in asp.net 5 C# web api resolved professionally and stay updated to the latest C# updates.