Reputation: 101
trying to upload a file to an already created folder but this error keeps coming
Access to the path 'C:\Users\Joe\Documents\Visual Studio 2015\Projects\training\Site1\Site1\Content\ProductImages' is denied.
and then below is this
Exception Details: System.UnauthorizedAccessException: Access to the path 'C:\Users\Joe\Documents\Visual Studio 2015\Projects\training\Site1\Site1\Content\ProductImages' is denied.
ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via , the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.
To grant ASP.NET access to a file, right-click the file in File 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.
Source Error:
Line 73: var path = Server.MapPath("~/Content/ProductImages"); // guardo en la variable path la direccion donde quiero guardar las imagenes, este path es un string que tu lo conformas segun el interes, puedeser upload/nombre_fichero/fecha/etc
Line 74: string pathdir = Path.Combine(path, imageName);
*Line 75: file.SaveAs(path); // store file*
Line 76: var imagen = new Image();
Line 77: imagen.ImagePath = pathdir;
and line 75 is in red When checking the granted rigths there is no restriction for storing or in the file to be stored
Upvotes: 0
Views: 2322
Reputation: 11
Having the path with just folder name and without file name results in Access Denied error. Check whether the filename is not empty/null.
Upvotes: 0
Reputation: 2361
The security is coming from the OS, not the compiler. Navigate to your folder in Windows, right-click, change security.
If deploying, be sure to use a folder that is automatically granted write permissions to .NET - like the User/Appdata/Roaming folder.
EDIT: You are saying you've added EVERYONE to the folder and given them full control but are still getting no joy. Please try this test:
private static void writeText()
{
using (TextWriter tw = new StreamWriter(@"C:myTestFile.txt"))
{
tw.WriteLine("Hello World",false);
tw.Close();
}
}
Run the above from your solution, then check you DEBUG folder for your solution (I've forgotten at this point if we're dealing with console or form or web, so you may have to search your computer for the mytestFile.txt file after running).
The point in running this is that C: (with no backslash) points to your project folder and will automatically have adequate permissions for write. If this works and you get a mytestFile.txt after running, then we go in one direction. If you get an access error, we go in another direction. So please run in your solution or a new console app (your solution would be best) and report back with the results. Have patience buddy - we'll do the best we can to get you where you need to be!
Upvotes: 1
Reputation: 301
Check the permission of that folder, right click on the folder, remove selection from the check box next to "Read Only", then click on "Security" => "Edit" => Select "Users" and tick the check box for "Full Control" and "Apply"
Upvotes: 0
Reputation: 9
We have to give write permissions to "IIS_Users" for folder "ProductImages" which are trying to upload, since IIS used this user to access your upload folder. Please see here how to add IIS_Users to your folder. Hope this helps you.
Upvotes: 0
Reputation: 166
Try to check the Sharing type of the folder.
Right click->Properties->Sharing Tab->Click Share... Buttong->Then Choose Everyone then add Then change it to read and write.
Then test your program again.
if error still exist try to add the folder to your solution explorer.
Click Solution Explorer->Show All files->Then Find the folder where it was then Include in the project.
Just give it a try.
Upvotes: 0
Reputation: 1008
The file permission error is coming from your Windows OS Explorer.
It gives you the exact fix:
To grant ASP.NET access to a file, right-click the file in File 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.
Give this a shot.
Upvotes: 0
Reputation: 479
When you host an application in IIS, IIS will run the application under another user name depending on your application pool settings. It is a good idea to know the Application Pool (What is an IIS application pool?), but in your specific case, you do not have to go through the learning curving.
The easiest way is to figure out the actual user name IIS is using by printing out the System.Environment.UserName and grant this user to the directory you want it to access.
Upvotes: 0