即使文件已保存,HttpPostedFile.SaveAs()也会抛出UnauthorizedAccessException?

时间:2010-04-16 18:33:48

标签: asp.net .net-3.5 iis-7

我有一个包含多个FileUpload控件和一个上传按钮的aspx页面。在点击处理程序中,我保存了这样的文件:

string path = "...";
for (int i = 0; i < Request.Files.Count - 1; i++)
{
    HttpPostedFile file = Request.Files[i];
    string fileName = Path.GetFileName(file.FileName);
    string saveAsPath = Path.Combine(path, fileName);
    file.SaveAs(saveAsPath);
}

调用file.SaveAs()时,会抛出:

  

System.Web.HttpUnhandledException:   类型异常   'System.Web.HttpUnhandledException'   被扔了。 ---&GT;   System.UnauthorizedAccessException的:   访问路径   '...'   被拒绝。在   System.IO .__ Error.WinIOError(的Int32   errorCode,String maybeFullPath)at   System.IO.FileStream.Init(String path,   FileMode模式,FileAccess访问,   Int32权限,布尔值使用权限,   FileShare共享,Int32 bufferSize,   FileOptions选项,   SECURITY_ATTRIBUTES secAttrs,String   msgPath,布尔bFromProxy)at   System.IO.FileStream..ctor(字符串   path,FileMode模式,FileAccess   访问,FileShare共享,Int32   bufferSize,FileOptions选项,   String msgPath,Boolean bFromProxy)
  在System.IO.FileStream..ctor(String   path,FileMode模式)at   System.Web.HttpPostedFile.SaveAs(字符串   文件名)   Belden.Web.Intranet.Iso.Complaints.AttachmentUploader.btnUpload_Click(对象   发件人,EventArgs e)at   System.Web.UI.WebControls.Button.OnClick(EventArgs的   吃   System.Web.UI.WebControls.Button.RaisePostBackEvent(字符串   eventArgument)at   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler   sourceControl,String eventArgument)
  在   System.Web.UI.Page.ProcessRequestMain(布尔   includeStagesBeforeAsyncPoint,Boolean   includeStagesAfterAsyncPoint)---   内部异常堆栈跟踪结束---   在   System.Web.UI.Page.HandleError(例外   吃   System.Web.UI.Page.ProcessRequestMain(布尔   includeStagesBeforeAsyncPoint,Boolean   includeStagesAfterAsyncPoint)at   System.Web.UI.Page.ProcessRequest(布尔   includeStagesBeforeAsyncPoint,Boolean   includeStagesAfterAsyncPoint)at   System.Web.UI.Page.ProcessRequest()
  在   System.Web.UI.Page.ProcessRequest(HttpContext的   上下文)   ASP.departments_iso_complaints_uploadfiles_aspx.ProcessRequest(HttpContext的   上下文)   System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()   在   System.Web.HttpApplication.ExecuteStep(IExecutionStep   步,布尔&amp; completedSynchronously)

现在这是有趣的部分。文件保存正确!那为什么要抛出这个例外?

更新

我通过检查非零ContentLength来修复它:

string path = "...";
for (int i = 0; i < Request.Files.Count - 1; i++)
{
    HttpPostedFile file = Request.Files[i];
    if (file.ContentLength == 0)
    {
        continue;
    }

    string fileName = Path.GetFileName(file.FileName);
    string saveAsPath = Path.Combine(path, fileName);
    file.SaveAs(saveAsPath);
}

3 个答案:

答案 0 :(得分:2)

尝试使用

file.SaveAs(server.mappath(saveAsPath));

答案 1 :(得分:2)

我通过检查非零ContentLength来修复它:

string path = "...";
for (int i = 0; i < Request.Files.Count - 1; i++)
{
    HttpPostedFile file = Request.Files[i];
    if (file.ContentLength == 0)
    {
        continue;
    }

    string fileName = Path.GetFileName(file.FileName);
    string saveAsPath = Path.Combine(path, fileName);
    file.SaveAs(saveAsPath);
}

有时我忽略了简单的事情......

答案 2 :(得分:1)

有意思......我的第一个问题是,你是否绝对确定真的是抛出异常的代码行?

两个......如果你(暂时)授予所有人访问该路径的权利,它会消失吗?

您的权限现在如何设置?什么用户运行ASP.NET?你在冒充吗?

相关问题