HttpPostedFile.SaveAs() throws UnauthorizedAccessException even though the file is saved?

Posted by jrummell on Stack Overflow See other posts from Stack Overflow or by jrummell
Published on 2010-04-16T18:33:47Z Indexed on 2010/04/16 18:43 UTC
Read the original article Hit count: 304

Filed under:
|
|

I have an aspx page with multiple FileUpload controls and one Upload button. In the click handler I save the files like this:

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

When file.SaveAs() is called, it throws:

System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.UnauthorizedAccessException: Access to the path '...' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode) at System.Web.HttpPostedFile.SaveAs(String filename) at Belden.Web.Intranet.Iso.Complaints.AttachmentUploader.btnUpload_Click(Object sender, EventArgs e) at System.Web.UI.WebControls.Button.OnClick(EventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) --- End of inner exception stack trace --- at System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.departments_iso_complaints_uploadfiles_aspx.ProcessRequest(HttpContext context) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Now here's the fun part. The file is saved correctly! So why is it throwing this exception?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about .net-3.5