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 ?
To grant ASP.NET access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.
ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.
To add permissions to a directory, perform the following steps: Click the "Add" button and enter the machine name followed by the ASPNET account name. For example, on a machine named "webdev", you would enter webdevASPNET and hit "OK".
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With