Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the error displaying in the screen while uploading:-

Error:-
System.UnauthorizedAccessException: Access to the path 'C:\inetpub\vhosts\nairsonly.com\httpdocs\nair_matrimony_pics\images.jpg' is denied.

Please help me here is my code...

C#
protected void Button1_Click(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        i++;
        if (upimage.HasFile)
        {
            if (CheckFileType(upimage.FileName))
            {

                string ext = Path.GetExtension(upimage.FileName);
                string filename = Path.GetFileName(upimage.FileName);
                string name = Server.MapPath("~/upload/") + s + i + ext;
                upimage.SaveAs(name);
                SqlDataSource1.InsertParameters["id"].DefaultValue = id;
                SqlDataSource1.InsertParameters["path"].DefaultValue = name;
                SqlDataSource1.Insert();
            }

        }

    }



    bool CheckFileType(string filename)
    {
        string ext = Path.GetExtension(filename);
        switch (ext.ToLower())
        {
            case ".gif":
                return true;
            case ".png":
                return true;
            case ".jpg":
                return true;
            case ".jpeg":
                return true;
            default:
                return false;
        }
    }

    void Page_PreRender()
    {
        string upfolder = MapPath("~/upload");
        DirectoryInfo dir = new DirectoryInfo(upfolder);
        DataList1.DataSource = dir.GetFiles();
        DataList1.DataBind();
    }

    protected void Delete_File(object sender, DataListCommandEventArgs Args)
    {

        File.Delete(Server.MapPath("~/upload")+ Args.CommandName);
        Page_PreRender();

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Class1.i = 0;
    }
        public Int32 return_number_class()
{
Class1 s = new Class1();
return s.return_number();
}
    public class Class1
{
        public Class1()
        {

        }
    public  static Int32 i =0 ;
    public Int32 return_number()
    {
        i++;
        return i;
    }

}
Posted
Updated 3-Apr-11 23:46pm
v2

You need to grant access the the ~/upload/ folder on the server so the ASP process is allowed to write in that folder.

Right click the ~/upload/ folder in Windows Explorer and choose properties. Go to the security tab and add the user group called IIS_WPG. Grand modify permissions to that group and you're good!

Eduard
 
Share this answer
 
Hello Sajna,

You are right the code is ok ,it will not give you error on your local machine due to gaining folder access credentials on your local machine.

You can try this simple trick

Open the IIS Manager on client server machine.Right Click on the folder called Upload and then go to security tab give the necessary permission to this folder to write and read both.
 
Share this answer
 

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