I keep on getting the same error in uploading a image on the published project in IIS: "UnauthorizedAccessException: Access to the path 'C:\inetpub\wwwroot\TestProj\wwwroot\files\clients\1111_64d96158-2a74-4277-98ed-7b12ba290b2d_CJQYABUT-SAMPLE-ID.jpg' is denied."
I keep on chaging the codes from _webHostEnvironment.WebRootPath and ContentRootPath, i tried using var uniqueFileName = "wwwroot/files/clients/";
and still dont work.
Here are some of the codes i tried,
string uniqueFileName = null;
if (client.FirstFile != null)
{
string uploadsFolder ="wwwroot/files/clients/";
uniqueFileName = client.Number "_" Guid.NewGuid().ToString() "_" client.FirstFile.FileName;
string filePath = Path.Combine(uploadsFolder, uniqueFileName);
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await client.FirstFile.CopyToAsync(fileStream);
}
client.FilePath = "/files/UploadImages/" uniqueFileName;
}
string uniqueFileName = null;
if (client.FirstFile != null)
{
string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "files/clients/");
uniqueFileName = client.Number "_" Guid.NewGuid().ToString() "_" client.FirstFile.FileName;
string filePath = Path.Combine(uploadsFolder, uniqueFileName);
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await client.FirstFile.CopyToAsync(fileStream);
}
client.FilePath = "/files/UploadImages/" uniqueFileName;
}
string uniqueFileName = null;
if (client.FirstFile != null)
{
string uploadsFolder = Path.Combine(_webHostEnvironment.ContentRootPath, "wwwroot/files/clients/");
uniqueFileName = client.Number "_" Guid.NewGuid().ToString() "_" client.FirstFile.FileName;
string filePath = Path.Combine(uploadsFolder, uniqueFileName);
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await client.FirstFile.CopyToAsync(fileStream);
}
client.FilePath = "/files/UploadImages/" uniqueFileName;
}
I don't know if I'm missing something, please help. Thanks
CodePudding user response:
The "UnauthorizedAccessException" error message you're seeing is indicating that the user account that the IIS worker process is running under does not have permission to access the specified file path.
There are a few things you can try to resolve this issue:
Make sure that the user account that the IIS worker process is running under (usually "IIS_IUSRS") has read and write access to the folder that you're trying to save the image in.
If you are using a version of IIS less than 8.5, you may need to configure the application pool to run as the "LocalSystem" account, which has the necessary permissions to access the folder.
You can also try to give the permissions for the folder to the "Everyone" group to confirm it's not a permission issue. But keep in mind that giving permissions to the "Everyone" group could be a security risk.
If you're still facing the same issue, you could try checking the file and folder permissions for the folder in Windows.
Another alternative is change the location of the image to be stored, to a folder outside the IIS.
Make sure that after you've made any changes to the file or folder permissions, you restart the IIS worker process for the changes to take effect.