无法使用ASP.NET文件上载保存文件-访问被拒绝

无法使用ASP.NET文件上载保存文件-访问被拒绝,asp.net,vb.net,file-upload,access-denied,Asp.net,Vb.net,File Upload,Access Denied,下面的代码用于我的网站插入照片,在我的电脑上,我可以插入照片,但当我将其传输到远程机器时,我会收到错误消息。代码如下: Protected Sub dvPictureInsert_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles dvPictureInsert.ItemInserted 'If the record

下面的代码用于我的网站插入照片,在我的电脑上,我可以插入照片,但当我将其传输到远程机器时,我会收到错误消息。代码如下:

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 > 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 & pictureIDJustAdded & ".jpg")
        End If
以下是错误消息:

“/please good”中的服务器错误 应用访问路径 'D:\Hosting\4423045\html\please god\UploadedImages\3.jpg' 他被拒绝了。描述:一个未处理的 测试期间发生异常 当前web请求的执行。 请查看堆栈跟踪以了解更多信息 有关错误和位置的信息 它起源于代码

例外情况详情: System.UnauthorizedAccess异常: 访问路径 'D:\Hosting\4423045\html\please god\UploadedImages\3.jpg' 他被拒绝了

ASP.NET无权访问 请求的资源。考虑 授予对资源的访问权限 到ASP.NET请求标识。 ASP.NET具有基本进程标识 (通常是IIS 5上的{MACHINE}\ASPNET 或IIS 6)上的网络服务 如果应用程序不可用,则使用 模仿。如果申请是 通过模拟,身份将 是匿名用户(通常是 IUSR_MACHINENAME)或经过身份验证的 请求用户

要授予ASP.NET对文件的访问权限, 在资源管理器中的文件上单击鼠标右键, 选择“属性”并选择 安全选项卡。单击“添加”以添加 适当的用户或组。突出 打开ASP.NET帐户,然后检查 用于所需访问的框

源错误:

第21行:如果 imageUpload.HasFile然后是第22行:
Dim baseDirectory作为字符串= Server.MapPath(“~/UploadedImages/”) 第23行:
imageUpload.SaveAs(基本目录& pictureIDJustAdded&“.jpg”)第24行: 如果第25行结束:如果源结束 文件: D:\Hosting\4423045\html\please god\PhotoAdmin\Default.aspx.vb 第23行

堆栈跟踪:

[未经授权的访问例外:访问 通往小路 'D:\Hosting\4423045\html\please god\UploadedImages\3.jpg' 被拒绝。]
System.IO.\uu错误.WinIOError(Int32 错误代码,字符串可能为空(路径) +7715167 System.IO.FileStream.Init(字符串路径, FileMode模式,FileAccess访问, Int32权限,布尔用户权限, 文件共享共享,Int32 bufferSize, 文件选项, 安全属性secAttrs,字符串 msgPath,布尔bFromProxy)+1162
System.IO.FileStream..ctor(字符串 路径、文件模式、文件访问 访问,文件共享,Int32 缓冲区大小、文件选项、, 字符串msgPath,布尔bFromProxy) +61 System.IO.FileStream..ctor(字符串 路径,文件模式)+55
System.Web.HttpPostedFile.SaveAs(字符串 文件名)+99
System.Web.UI.WebControls.FileUpload.SaveAs(字符串 文件名)+23
PhotoAdmin_Default.dvPictureInsert_ItemInserted(对象 发件人,详细信息viewinsertedeventargs e) 在 D:\Hosting\4423045\html\please god\PhotoAdmin\Default.aspx.vb:23 System.Web.UI.WebControls.DetailsView.OnItemInserted(DetailsViewInsertedEventArgs e) +108
System.Web.UI.WebControls.DetailsView.HandleInsertCallback(Int32 受影响的道路,例外情况ex)+69
System.Web.UI.DataSourceView.Insert(IDictionary 价值观 DataSourceViewOperationCallback 回调)+134
System.Web.UI.WebControls.DetailsView.HandleInsert(字符串 commandArg,布尔原因验证) +274 System.Web.UI.WebControl.DetailsView.HandleEvent(事件参数 e、 布尔原因验证,字符串 验证组)+676
System.Web.UI.WebControl.DetailsView.OnBubbleEvent(对象 来源,事件参数e)+95
System.Web.UI.Control.RaiseBubbleEvent(对象 来源,事件args args)+37
System.Web.UI.WebControl.DetailsView.OnBubbleEvent(对象 来源,事件参数e)+113
System.Web.UI.Control.RaiseBubbleEvent(对象 来源,事件args args)+37
System.Web.UI.WebControl.LinkButton.OnCommand(CommandEventArgs e) +118
System.Web.UI.WebControl.LinkButton.RaisePostBackEvent(字符串 事件参数)+135
System.Web.UI.WebControl.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串 事件参数)+10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,字符串事件参数) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)+175
System.Web.UI.Page.ProcessRequestMain(布尔值 includeStagesBeforeAsyncPoint,布尔值 includeStagesAfterAsyncPoint)+1565


您必须确保IIS或运行网站的服务器具有写入该目录的权限。 您可以通过选择该文件夹->属性->安全选项卡来完成此操作

如果这没有帮助,您可以在事件日志中重新检查,以找出它试图使用哪个帐户访问它


您可能还想阅读帖子。

即使您将应用程序池设置为具有正确权限的用户,如果您在web.config中打开了模拟,您也会收到此错误

从web.config中删除此行将关闭模拟

<identity impersonate="true" />

今天我遇到了同样的问题,我在本地和live server上测试了它,但同样的错误让我很沮丧。然后我决定批判性地检查我的
代码
,以便找出问题的原因。问题是,我没有在fileupload
SaveAs()方法中指定文件名。我知道op正在指定一条正确的路径,但它可能在将来发生在其他人身上

我的代码以前的样子:
fileupload.SaveAs(Server.MapPath($“{basedir}”)

现在的样子:
fileupload.SaveAs(Server.MapPath($“{basedir}/{fileupload.FileName}”)

可能存在的重复项