title: Unable to save file with ASP.NET FileUpload - access denied
body: <p>the below code is use for my website to insert photos, On my compouter, i can insert pics but when i transfer it to the remote machine, i get error message. Here is the code:</p>
  
  <pre><code>Protected Sub dvPictureInsert_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles dvPictureInsert.ItemInserted
  
      'If the record was successfully inserted, save the picture
      If e.AffectedRows &gt; 0 Then
  
          'Determine the maximum pictureID for this user
          Dim results As DataView = CType(maxPictureIDDataSource.Select(DataSourceSelectArguments.Empty), DataView)
          Dim pictureIDJustAdded As Integer = CType(results(0)(0), Integer)
          'Reference the FileUpload control
          Dim imageUpload As FileUpload = CType(dvPictureInsert.FindControl("imageUpload"), FileUpload)
  
          If imageUpload.HasFile Then
              Dim baseDirectory As String = Server.MapPath("~/UploadedImages/")
              imageUpload.SaveAs(baseDirectory &amp; pictureIDJustAdded &amp; ".jpg")
          End If
  </code></pre>
  
  <p>Here is the error message:</p>
  
  <blockquote>
    <p>Server Error in '/please-god'
    Application. Access to the path
    'D:\Hosting\4423045\html\please-god\UploadedImages\3.jpg'
    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. </p>
    
    <p>Exception Details:
    System.UnauthorizedAccessException:
    Access to the path
    'D:\Hosting\4423045\html\please-god\UploadedImages\3.jpg'
    is denied. </p>
    
    <p>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. </p>
    
    <p>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.</p>
    
    <p>Source Error: </p>
    
    <p>Line 21:             If
    imageUpload.HasFile Then Line 22:<br>
    Dim baseDirectory As String =
    Server.MapPath("~/UploadedImages/")
    Line 23:<br>
    imageUpload.SaveAs(baseDirectory &amp;
    pictureIDJustAdded &amp; ".jpg") Line 24: 
    End If Line 25:         End If Source
    File:
    D:\Hosting\4423045\html\please-god\PhotoAdmin\Default.aspx.vb
    Line: 23 </p>
    
    <p>Stack Trace: </p>
    
    <p>[UnauthorizedAccessException: Access
    to the path
    'D:\Hosting\4423045\html\please-god\UploadedImages\3.jpg'
    is denied.]<br>
    System.IO.__Error.WinIOError(Int32
    errorCode, String maybeFullPath)
    +7715167    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) +1162<br>
    System.IO.FileStream..ctor(String
    path, FileMode mode, FileAccess
    access, FileShare share, Int32
    bufferSize, FileOptions options,
    String msgPath, Boolean bFromProxy)
    +61    System.IO.FileStream..ctor(String
    path, FileMode mode) +55<br>
    System.Web.HttpPostedFile.SaveAs(String
    filename) +99<br>
    System.Web.UI.WebControls.FileUpload.SaveAs(String
    filename) +23<br>
    PhotoAdmin_Default.dvPictureInsert_ItemInserted(Object
    sender, DetailsViewInsertedEventArgs
    e) in
    D:\Hosting\4423045\html\please-god\PhotoAdmin\Default.aspx.vb:23
    System.Web.UI.WebControls.DetailsView.OnItemInserted(DetailsViewInsertedEventArgs
    e) +108<br>
    System.Web.UI.WebControls.DetailsView.HandleInsertCallback(Int32
    affectedRows, Exception ex) +69<br>
    System.Web.UI.DataSourceView.Insert(IDictionary
    values,
    DataSourceViewOperationCallback
    callback) +134<br>
    System.Web.UI.WebControls.DetailsView.HandleInsert(String
    commandArg, Boolean causesValidation)
    +274    System.Web.UI.WebControls.DetailsView.HandleEvent(EventArgs
    e, Boolean causesValidation, String
    validationGroup) +676<br>
    System.Web.UI.WebControls.DetailsView.OnBubbleEvent(Object
    source, EventArgs e) +95<br>
    System.Web.UI.Control.RaiseBubbleEvent(Object
    source, EventArgs args) +37<br>
    System.Web.UI.WebControls.DetailsViewRow.OnBubbleEvent(Object
    source, EventArgs e) +113<br>
    System.Web.UI.Control.RaiseBubbleEvent(Object
    source, EventArgs args) +37<br>
    System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs
    e) +118<br>
    System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String
    eventArgument) +135<br>
    System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
    eventArgument) +10<br>
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
    sourceControl, String eventArgument)
    +13    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection
    postData) +175<br>
    System.Web.UI.Page.ProcessRequestMain(Boolean
    includeStagesBeforeAsyncPoint, Boolean
    includeStagesAfterAsyncPoint) +1565</p>
  </blockquote>