I am trying to upload images to a website i have hosted. It work fine on local machine, but give error while uploading to hosted website. It is built on Asp.Net Core 6.
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(ProductVM objVM, IFormFile? file)
{
try
{
if (ModelState.IsValid)
{
string wwwRootPath = _hostnvironment.WebRootPath;
if (file != null)
{
string filename = Guid.NewGuid().ToString();
var uploads = Path.Combine(wwwRootPath, "Images", "Product");
var extention = Path.GetExtension(file.FileName);
var fullpath = Path.Combine(wwwRootPath, uploads, filename + extention);
using (var fileStreams = new FileStream(fullpath, FileMode.Create))
{
file.CopyTo(fileStreams);
}
objVM.ImageURL = @"/Images/Product/" + filename + extention;
}
var _product = (Product)objVM;
try
{
_db.Products?.Add(_product);
_db.SaveChanges();
TempData["success"] = "Product Created Successfully";
return RedirectToAction("Index");
}
catch { }
}
}
catch (Exception ex)
{
//Error = Access to the path //'E:Inetpubvhostsmywebsite.inhttpdocswwwrootImagesProduct8033d927135.png' is denied.
}
return View(objVM);
}
i searched for the code to change permissions of the folder, but unable find a working code