10


When i publish my project to iis and i do upload a picture on browser so this operation is fails and show this error in logger sysytem

An unhandled exception has occurred: Access to the path 'C:\Inetpub\vhosts\qarbal.com\back.qarbal.com\wwwroot\images\UserProfile\pic_50.jpg' is denied.System.UnauthorizedAccessException: Access to the path 'C:\Inetpub\vhosts\qarbal.com\back.qarbal.com\wwwroot\images\UserProfile\pic_50.jpg' is denied. at System.IO.FileStream.OpenHandle(FileMode mode, FileShare share, FileOptions options) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)

ActionController

[HttpPost("[action]")]
public async Task<IActionResult> EditPhoto()
{
    var uploadsRootFolder = Path.Combine(_env.WebRootPath, "images\\UserProfile");
    var files = Request.Form.Files;     
    foreach (var file in files)
    {       
        if (file == null || file.Length == 0)
        {
            continue;
        }
        var filePath = Path.Combine(uploadsRootFolder, queryModel.Name  + files[0].FileName);
        using (var fileStream = new FileStream(filePath, FileMode.Create))
        {
            await file.CopyToAsync(fileStream).ConfigureAwait(false);
        }
    }
    return Json("ok");  
}

startup.cs

public void Configure(ILoggerFactory loggerFactory, IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseStaticFiles();
}

How to solve this Exception ?

2
  • It looks like your app pool doesn't have enough permissions. Commented Feb 22, 2018 at 17:10
  • Most likely the app pool identity doesn't have access to that path. Try giving it access to c:\inetpub (and all of its sub-directories). If the app pool runs as an impersonated user, you should pick a different folder than inetpub. Commented Feb 22, 2018 at 17:11

2 Answers 2

19

Right click on the wwwroot folder -> Properties -> Security tab -> Click at Edit button -> Enter IIS AppPool\DefaultAppPool user -> Click at Check names -> OK -> Then give it Write permission.

Sign up to request clarification or add additional context in comments.

4 Comments

not working, those this changes need to restart the server or?
This worked for me except the user name was called IIS IUSRS, which I found listed under the Advanced button in the Add user menu.
Isnt it risky to give eveyrthing write permission?
It's not everything. It's only the user of your app's pool in the IIS. The rest of it should be controlled by your app.
-2

Work fine...

string fileName = file_name+".pdf";

           string files = System.IO.Path.Combine("pdf/") + fileName;
            if (!System.IO.File.Exists(files))
            {
                return RedirectToAction("index", new { message = "File Not Found" });

            }
            else
            {

                byte[] fileBytes = System.IO.File.ReadAllBytes(files);
                return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, files);

            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.