简体   繁体   中英

What permissions are required to upload to WWWRoot folder in ASP.NET Core API

I have created asp.net core 2.2 web API + MVC project and I am trying to upload a file to WWWRoot folder. I get access denied error when trying to upload file.

I would like to know appropriate accounts and required permissions to enable write access

I have applied UseStaticFiles in Startup file.

Here's code to upload file:

  public async Task<IActionResult> Create(FileuploadRequest fileuploadRequest)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest();
            }
            else
            {

                if (fileuploadRequest.Logo.Length > 0)
                {
                    string webRootPath = _hostingEnvironment.WebRootPath;

                    var folderPath = Path.Combine(webRootPath,"Resources\\Images");

                    using (var fileStream = new FileStream(folderPath, FileMode.Create))
                    {
                        await fileuploadRequest.Logo.CopyToAsync(fileStream);

                    }

Using below of code work for me

public async Task<string> UploadAssets(IFormFile file, string fileName)
    {
        string storePath = "";
        try
        {
            storePath = Path.Combine(_assetSettings.Value.StorePath, $"{fileName}.{file.ContentType.Split("/")[1]}");
            using (var stream = new FileStream(storePath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }
         }
  }

Also try to run your Visual studio in Administrator right to allow full access to file, folder in your computer

As Joe suggested ,make sure that the folder exists and try to append the fileName to your folder path like below:

 if (fileuploadRequest.Logo.Length > 0)
            {
                string webRootPath = _hostingEnvironment.WebRootPath;

                var fileName = Path.GetFileName(fileuploadRequest.Logo.FileName);

                var folderPath = Path.Combine(webRootPath,"Resources\\Images",fileName);

                using (var fileStream = new FileStream(folderPath, FileMode.Create))
                {
                    await fileuploadRequest.Logo.CopyToAsync(fileStream);

                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM