简体   繁体   中英

ASP.Net uploading image to wwwroot folder

I keep on getting the same error in uploading a image on the published project in IIS: "UnauthorizedAccessException: Access to the path 'C:\.netpub\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

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.

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