asp.net-corefile-upload

How to upload image file to wwwroot folder in ASP.NET Core 6 and above


I am trying to upload images to a website I have hosted. It works fine on my local machine, but I get an error while uploading to hosted website. It is built to run 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 extension = Path.GetExtension(file.FileName);

                var fullpath = Path.Combine(wwwRootPath, uploads, filename + extension);

                using (var fileStreams = new FileStream(fullpath, FileMode.Create))
                {
                    file.CopyTo(fileStreams);
                }

                objVM.ImageURL = @"/Images/Product/" + filename + extension;
            }

            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:\Inetpub\vhosts\mywebsite.in\httpdocs\wwwroot\Images\Product\8033d927135.png' is denied.

    }
    
    return View(objVM);
}

i searched for the code to change permissions of the folder, but unable find a working code


Solution

  • According to the error message, its very clearly that your application don't have the enough permission to access that E path to store the file.

    To solve this issue, I suggest you could try below steps:

    If you directly running the application inside the server instead of using the IIS.

    Please run the application with administrator role.

    If you host this application inside the IIS.

    Please follow this step to modify the IIS application pool identity to localsystem to see if this has been solved or not.

    If modify the application pool identity solve the issue, this means the identtiy pool doesn't contain the enough permission, since using the localsytstem is not security enough inside the product environment, I suggest you could modify the E:\Inetpub\vhosts\mywebsite.in\httpdocs\wwwroot\ folder to set enough permission for the IIS hosted application pool like below:

    1.Open Windows Explorer

    2.Select a file or directory.

    3.Right click the file and select Properties

    4.Select the Security tab

    5.Click the Edit button and then Add button

    6.Click the Locations button and make sure that you select your computer.

    7.Enter IIS AppPool\yourapplicationpoolname in the Enter the object names to select: text box.

    enter image description here

    8.Click the Check Names button and click OK.

    9.Screenshot of the Select Users or Groups dialog.

    By doing this, the file or directory you selected will now also allow the yourapplicationpool identity access.