Click here to Skip to main content
15,925,868 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having a problem in displaying an image on my web page.

I always get:
UnauthorizedAccessException: Access to the path D:\ASP.NET Practice\WebControls\Images is denied.

This is the code that i am using:
C#
string Url, filename;
byte[] imageBytes;
filename = FUload.FileName;
Url = HttpContext.Current.Server.MapPath("~/Images" );
imageBytes = FUload.FileBytes;
File.WriteAllBytes(Url, imageBytes);
Img.ImageUrl = Url + filename;


I have set secutiry permission for ASP.NET user but it still gives the same problem.

Please, can anyone help me in resolving this issue?
Thanks
Posted
Updated 29-Jul-10 5:54am
v3

Error is pretty straight forward. It's a security permissions issue.

Are you using any kind of 'impersonation'? Either in your Web.Config file or in IIS? If so, then you need to give that user correct permissions.
In case of impersonation, ASPNET is not the one accessing the path.

BTW: there is a typo (or mistake?) in line:
C#
Url = HttpContext.Current.Server.MapPath("~/Images" );


Forward slash is missing at the end. It should be:
C#
Url = HttpContext.Current.Server.MapPath("~/Images/" );
 
Share this answer
 
v2
Comments
Jondo24shoots 29-Jul-10 12:46pm    
Thanks for your concern but i've gotten another way
to do it and i've posted it
Sandeep Mewara 29-Jul-10 13:22pm    
Ok. But I have updated my answer. Have a loot at it. I think, which looked to me a typo earlier was your mistake. 'Forward Slash' was missing.,.. try that, it should work too.
I got another way that did not give all those
security problems

this is the code below

string filename,ext ,fullname;

Random guid = new Random();

fullname = FUload.FileName;
ext = Path.GetExtension(fullname);
filename = guid.ToString() + ext;

FUload.SaveAs(MapPath("~/Images/" + filename ));

Img.ImageUrl = "~/Images/" + filename;
 
Share this answer
 
Comments
Yusuf 29-Jul-10 13:56pm    
Well, look beyond your code. Make sure ASP.NET has permission set at the folder level.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900