AjaxControlToolkit 异步文件上传器无法正常工作

发布于 2024-12-09 01:48:51 字数 1244 浏览 0 评论 0原文

我的页面上有一个 AsynchFileUpload ,由于某种原因,当我尝试使用它将文件保存到服务器时,它失败了。

该控件允许我在本地选择一个文件,并在其文本框中显示本地文件路径,但是当我单击页面上的按钮时,我将使用该按钮提交所有详细信息,然后上传页面,一切都会出错我从 AsynchFileUploader 收到空引用异常。

我的上传器相当基本,看起来像:

   <cc1:AsyncFileUpload runat="server" 
                                  ID="AsyncFileUpload2" 
                                  Enabled="true" 
                                  Visible="true"/>

上传器位于选项卡容器/选项卡面板/内容模板/更新面板中,更新模式设置为有条件。我对 ASP 相当陌生,所以我不确定包含上传器的控件是否会导致问题。

然后在我的代码中,我有:

 Dim filename As String = System.IO.Path.GetFileName(AsyncFileUpload2.FileName)

        Dim comments As String = SpellTextBox1.Text


        Dim NewDirectory As String = Server.MapPath("~/Helpdesk/UploadedFiles/" + TicketID.ToString())

        Try



           'Check if directory exists
           If Not Directory.Exists(NewDirectory) Then

              ' Create the directory.
              Directory.CreateDirectory(NewDirectory)

           End If

           AsyncFileUpload2.SaveAs(NewDirectory + "\" + filename)

        Catch _ex As IOException
           'Silently error for now
           'Response.Write(_ex.Message)

        End Try

单击按钮后,文件名似乎在某处丢失,或者从未存储

I have an AsynchFileUpload on my page and for some reason when I try to use it to save the file to the server it falls.

The control will allow me to select a file locally and it displays the local file path in its text box, BUT when I then click a button on my page which I am going to be using to submit all details then upload the page everything goes wrong and I get a Null Ref Exception from the AsynchFileUploader.

My uploader is fairly basic and looks like:

   <cc1:AsyncFileUpload runat="server" 
                                  ID="AsyncFileUpload2" 
                                  Enabled="true" 
                                  Visible="true"/>

The uploader is within a tab container / tab panel / content template / update panel with the update mode set to conditional. Im fairly new to ASP so im not sure if the controls containing the uploader could be causing the problem.

Then in my code i have:

 Dim filename As String = System.IO.Path.GetFileName(AsyncFileUpload2.FileName)

        Dim comments As String = SpellTextBox1.Text


        Dim NewDirectory As String = Server.MapPath("~/Helpdesk/UploadedFiles/" + TicketID.ToString())

        Try



           'Check if directory exists
           If Not Directory.Exists(NewDirectory) Then

              ' Create the directory.
              Directory.CreateDirectory(NewDirectory)

           End If

           AsyncFileUpload2.SaveAs(NewDirectory + "\" + filename)

        Catch _ex As IOException
           'Silently error for now
           'Response.Write(_ex.Message)

        End Try

It seems that the filename is being lost somewhere after the button is clicked, or just never stored

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

留蓝 2024-12-16 01:48:51

通过执行以下操作解决:

Private Sub AsyncFileUpload1_UploadedComplete(ByVal sender As Object, ByVal e As AjaxControlToolkit.AsyncFileUploadEventArgs) Handles AsyncFileUpload1.UploadedComplete
      If e.State = AjaxControlToolkit.AsyncFileUploadState.Success Then

         Dim ticketID As String = Request.QueryString("ID").ToString()
         Dim filename As String = e.FileName

         Dim temp() As String = filename.Split("\")
         filename = temp(temp.Length - 1)


         Dim NewDirectory As String = Server.MapPath("~/Helpdesk/UploadedFiles/" + ticketID + "/")

         'Check if directory exists
         If Not Directory.Exists(NewDirectory) Then

            ' Create the directory.
            Directory.CreateDirectory(NewDirectory)

         End If

         ' Save the file on the server
         AsyncFileUpload1.SaveAs(NewDirectory + filename)

         'Now put the file details in the database
         dbSaveFile(ticketID, filename, "UploadedFiles/" + ticketID + "/" + filename, "")

         'Raise some kind of notification informing the user that this has been sucessfull



         'And rebind the control
         ' Get Event Details and populate text fields
         Dim dsFiles As DataSet = dbGetFiles(ticketID)

         If dsFiles Is Nothing Then
            ' No need to error here if we have no files
         Else
            gvFiles.DataSource = dbGetFiles(ticketID)
            gvFiles.DataBind()

         End If

      Else
         ' Do nothing
      End If
   End Sub

Solved by doing:

Private Sub AsyncFileUpload1_UploadedComplete(ByVal sender As Object, ByVal e As AjaxControlToolkit.AsyncFileUploadEventArgs) Handles AsyncFileUpload1.UploadedComplete
      If e.State = AjaxControlToolkit.AsyncFileUploadState.Success Then

         Dim ticketID As String = Request.QueryString("ID").ToString()
         Dim filename As String = e.FileName

         Dim temp() As String = filename.Split("\")
         filename = temp(temp.Length - 1)


         Dim NewDirectory As String = Server.MapPath("~/Helpdesk/UploadedFiles/" + ticketID + "/")

         'Check if directory exists
         If Not Directory.Exists(NewDirectory) Then

            ' Create the directory.
            Directory.CreateDirectory(NewDirectory)

         End If

         ' Save the file on the server
         AsyncFileUpload1.SaveAs(NewDirectory + filename)

         'Now put the file details in the database
         dbSaveFile(ticketID, filename, "UploadedFiles/" + ticketID + "/" + filename, "")

         'Raise some kind of notification informing the user that this has been sucessfull



         'And rebind the control
         ' Get Event Details and populate text fields
         Dim dsFiles As DataSet = dbGetFiles(ticketID)

         If dsFiles Is Nothing Then
            ' No need to error here if we have no files
         Else
            gvFiles.DataSource = dbGetFiles(ticketID)
            gvFiles.DataBind()

         End If

      Else
         ' Do nothing
      End If
   End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文