title: HttpPostedFile.SaveAs() throws UnauthorizedAccessException even though the file is saved?
body: <p>I have an aspx page with multiple <code>FileUpload</code> controls and one Upload button. In the click handler I save the files like this:</p>
  
  <pre><code>string path = "...";
  for (int i = 0; i &lt; 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);
  }
  </code></pre>
  
  <p>When <code>file.SaveAs()</code> is called, it throws:</p>
  
  <blockquote>
    <p>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)<br>
    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)<br>
    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()<br>
    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&amp; completedSynchronously)</p>
  </blockquote>
  
  <p>Now here's the fun part. The file is saved correctly! So why is it throwing this exception?</p>
  
  <p><strong>Update</strong></p>
  
  <p>I fixed it by checking for a non zero ContentLength:</p>
  
  <pre><code>string path = "...";
  for (int i = 0; i &lt; 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);
  }
  </code></pre>