我有一个按钮触发的文件上传控件。单击该按钮时,我正在检查目录是否存在,如果没有,则创建它并将文件从fileupload保存到它。运行应用程序时,如果该目录不存在,则会成功创建该目录,但该文件不会保存到该目录并抛出未授权的访问异常。我正在我的本地计算机上工作,网络服务帐户对我正在创建子目录的目录具有修改权限。我检查了创建文件夹的NTFS权限,并且它们从父级继承,因此看起来没问题。我在代码中做错了什么,或者不同的帐户是否需要对目录的权限? (我通过暂时授予'Everyone'修改对文件夹的访问权限进行测试,但出现了相同的错误)
代码:
If fuSupport.HasFile Then
Dim clockNo As String = lblClockNo.Text.Trim()
Dim employeePath As String = "~/SupportingFiles/" & clockNo
If Not Directory.Exists(Server.MapPath(employeePath)) Then
Directory.CreateDirectory(Server.MapPath(employeePath))
End If
Dim filePath As String = Path.GetFileName(fuSupport.PostedFile.FileName)
Dim fileName As String = Path.GetFileName(filePath)
Dim ext As String = Path.GetExtension(fileName)
fuSupport.SaveAs(Server.MapPath(employeePath))
End if
FileUpload Control:
<asp:UpdatePanel ID="upSupportingFiles" runat="server">
<ContentTemplate>
<asp:FileUpload ID="fuSupport" runat="server" Width="100%" /><br />
<asp:Label ID="lblUpload" runat="server"></asp:Label>
<br />
<table cellpadding="3" cellspacing="0" style="width: 100%">
<tr>
<td style="width:50px">
<asp:Button ID="btnUpload" runat="server" CssClass="buttonStyle" Text="Upload File" CausesValidation="False" />
</td>
<td style="padding-left:10px">
<asp:UpdateProgress ID="upUpload" runat="server">
<ProgressTemplate>
<asp:Image ID="imgAjaxLoader" runat="server" ImageUrl="~/App_Themes/APS/Images/ajax-loader.gif" />
</ProgressTemplate>
</asp:UpdateProgress>
</td>
</tr>
</table>
<br />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnUpload" />
</Triggers>
</asp:UpdatePanel>
答案 0 :(得分:0)
发现问题。
修订
fuSupport.SaveAs(Server.MapPath(employeePath))
到
fuSupport.SaveAs(Server.MapPath(employeePath & "/" & fileName))
现在正常工作