ASP.Net Error: Access to path is denied on hosting server

democloud
 
on Dec 03, 2019 12:20 AM
1878 Views

Hello Sir, 

Below is the code which i am using to create a folder at runtime and upload the files to the same, its working correctly on localhost, but after hosting im getting error

Error : Access to path is denied

Please help.

if (FileUpload1.HasFile)
{
    try
    {
        string folder_name = DropDownList1.SelectedItem.Text;
        string folderPath = Server.MapPath("~/" + DropDownList1.SelectedItem.Text + "/");

        //Check whether Directory (Folder) exists.
        if (!Directory.Exists(folderPath))
        {
            //If Directory (Folder) does not exists. Create it.
            Directory.CreateDirectory(folderPath);
        }

        //Save the File to the Directory (Folder).
        // FileUpload1.SaveAs(folderPath + Path.GetFileName(FileUpload1.FileName));

        // Get the HttpFileCollection
        HttpFileCollection hfc = Request.Files;
        for (int i = 0; i < hfc.Count; i++)
        {
            HttpPostedFile hpf = hfc[i];
            if (hpf.ContentLength > 0)
            {
                hpf.SaveAs(folderPath + "\\" +
                    Path.GetFileName(hpf.FileName));
            }
        }
    }
    catch (Exception ex)
    {
        // Handle your exception here
        throw ex;
    }
    ScriptManager.RegisterStartupScript(this, this.GetType(), "SweetAlert", "swal('Success!', 'Files uploaded successfully successfully.!', 'success');", true);
    DropDownList1.ClearSelection();
}
else
{
    ScriptManager.RegisterStartupScript(this, this.GetType(), "SweetAlert", "swal('Warning!', 'Please select files to be uploaded.!', 'warning');", true);
}

Thanks

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Dec 03, 2019 04:06 AM

Hi democloud,

Ask your hosting provider to grant aspnet user to read, write permission on wwwroot folder.

Else refer below link.

https://www.c-sharpcorner.com/forums/access-is-denied-error-when-folder-create-using-code