c# - 上传 excel 文件时访问路径被拒绝

标签 c# asp.net asp.net-3.5

我在上传 excel 文件时收到此错误消息,有人可以帮助我吗?

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.

我的代码隐藏文件具有以下语法

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");
                }
            }

最佳答案

确保文件夹 C:\Data\IronElements\Upload\AUMData 对执行 IIS 的用户具有 NTFS 写入权限。另外请确保 C:\Data\IronElements\Upload 的子文件夹继承了其父文件夹的权限。为此,请单击安全选项卡中的高级按钮 -> 更改权限 -> 检查将所有子对象权限替换为此对象的可继承权限 -> 点击确定

关于c# - 上传 excel 文件时访问路径被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4340074/

相关文章:

c#,当只有 1 个读取器时,我一直捕获多个 MySQL 读取器异常

c# - 如何调试 Silverlight 3 C# 应用程序?

c# - Internet Explorer 中的渐进式表呈现

c# - 将日期时间转换为字符串时出错

asp.net-3.5 - 如何验证作为 ASP.NET Web 应用程序一部分的 ASP.NET Web 服务的用户?

c# - 如何使用VisualState AdaptiveTrigger改变ListView Item FontSize

c# - IIS 错误 : Unrecognized configuration path 'MACHINE/WEBROOT/APPHOST/websiteName

.net - 我们如何在 aspx 页面的用户控件上使用必填字段验证器

c# - 如何在C#中刷新单例

visual-studio-2010 - 工厂从哪里来?