简体   繁体   中英

Access to the path Denied when uploading excel file

When uploading an excel file I am receiving this error, can anyone help me?

Access to the path 'C:\\Data\\IronElements\\Upload\\AUMData\\20101202 031815.xlsx' is denied. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.UnauthorizedAccessException: Access to the path 'C:\\Data\\IronElements\\Upload\\AUMData\\20101202 031815.xlsx' 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) 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 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:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

My Code behind file has the following syntax

DateTime date = DateTime.Now;
                string FileName = Convert.ToString(date.ToString("yyyyMMdd hhmmss"));
                Directory.CreateDirectory("C:\\Data\\IronElements\\Upload\\AUMData\\Schema");
                doesFileExists("C:\\Data\\IronElements\\Upload\\AUMData\\Schema");
                fileUpload.PostedFile.SaveAs("C:\\Data\\IronElements\\Upload\\AUMData\\" + FileName + ".xlsx");
                System.Threading.Thread.Sleep(5000);
                string connectionString = WebConfigurationManager.ConnectionStrings["SQLConnection"].ConnectionString;
                SqlConnection SqlConnect = new SqlConnection(connectionString);

                try
                {
                    SqlConnect.Open();
                    SqlCommand cmdAssetUnderManagement = new SqlCommand("Exec_Insert_AUMAssetValue", SqlConnect);
                    cmdAssetUnderManagement.CommandType = CommandType.StoredProcedure;
                    cmdAssetUnderManagement.ExecuteNonQuery();

                }


                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
                finally
                {
                    SqlConnect.Close();
                }
                lblAUMTA.Visible = true;
                lblAUMTA.Text = "File Upload Completed";
            }
    private void doesFileExists(string p)
            {
                p = string.Concat(p, "\\AUMSchema.xlsx");
                if (!File.Exists(p))
                {
                    fileUpload.PostedFile.SaveAs("C:\\Data\\IronElements\\Upload\\AUMData\\Schema\\AUMSchema.xlsx");
                }
            }

Make sure the folder C:\\Data\\IronElements\\Upload\\AUMData has NTFS write permission for the user in which context IIS executes. Also please make sure that the subfolders of C:\\Data\\IronElements\\Upload inherits permissions from it's parent. To do this, click Advanced button from security tab -> Change permissions -> check Replace all child object permissions with inheritable permissions from this object -> Hit Ok

go to the app-pool which you are using for this application and change the identity of it to 'Network Service'. I faced the similar issue and fixed in same way

The account that your web session is running under does not have permissions to write to that folder. Once you give the ASP.Net account (or whichever account you are using) the required permissions, it should work as expected.

To get a better handle on permission problems like this I always create a low-priv'ed user to run our websites. The user is named after the website itself so it's obvious what it's for.

Then that user only gets perms on the right folders, in your case, C:\\Data\\IronElements\\Upload\\AUMData. It's also a good way to limit DB access - create a login for that specific user in SQL with minimal privs.

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