【问题标题】:Fileupload - Able to create folder but not save file to itFileupload - 能够创建文件夹但不能将文件保存到其中
【发布时间】:2013-12-20 09:36:25
【问题描述】:

我有一个由按钮触发的文件上传控件。单击按钮时,我正在检查目录是否存在,如果不存在,则创建它并将文件从 fileupload 保存到它。运行应用程序时,如果目录不存在,则创建成功,但文件不会保存到目录并引发未经授权的访问异常。我在我的本地机器上工作,网络服务帐户对我在其中创建子目录的目录具有修改权限。我检查了创建的文件夹的 NTFS 权限,它们从父级继承,所以看起来没问题。我在我的代码中做错了什么还是其他帐户需要目录权限? (我通过临时授予“所有人”修改文件夹的访问权限进行了测试,但出现了同样的错误)

代码:

 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

文件上传控件:

<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>

【问题讨论】:

  • 你是这台机器的管理员吗?
  • 我是的。尝试右键单击 Visual Studio 并选择“以管理员身份运行”仍然是同样的错误

标签: asp.net vb.net file-upload


【解决方案1】:

找到问题了。

修改

fuSupport.SaveAs(Server.MapPath(employeePath))

fuSupport.SaveAs(Server.MapPath(employeePath & "/" & fileName))

现在工作正常

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 2021-10-10
    • 2017-06-10
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多