Asp.net AjaxControlToolkit异步文件上载程序工作不正常
我的页面上有一个AsynchFileUpload,由于某种原因,当我试图使用它将文件保存到服务器时,它会掉下来 控件将允许我在本地选择一个文件,并在其文本框中显示本地文件路径,但当我单击页面上的一个按钮时,我将使用该按钮提交所有详细信息,然后上载页面,所有内容都出错,我从AsynchFileUploader获得一个Null Ref异常 我的上传程序相当基本,看起来像:Asp.net AjaxControlToolkit异步文件上载程序工作不正常,asp.net,ajax,vb.net,ajaxcontroltoolkit,Asp.net,Ajax,Vb.net,Ajaxcontroltoolkit,我的页面上有一个AsynchFileUpload,由于某种原因,当我试图使用它将文件保存到服务器时,它会掉下来 控件将允许我在本地选择一个文件,并在其文本框中显示本地文件路径,但当我单击页面上的一个按钮时,我将使用该按钮提交所有详细信息,然后上载页面,所有内容都出错,我从AsynchFileUploader获得一个Null Ref异常 我的上传程序相当基本,看起来像: <cc1:AsyncFileUpload runat="server"
<cc1:AsyncFileUpload runat="server"
ID="AsyncFileUpload2"
Enabled="true"
Visible="true"/>
单击按钮后,文件名似乎在某个地方丢失,或者从未存储过,请执行以下操作解决此问题:
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
看看我对一个类似问题的回答,看看它是否有用:谢谢,我以前看过这个问题,我想我对这个控件的工作原理有点困惑,因为我认为UploadedComplete事件在我真正上传文件之前不会触发,因为我没有指定文件路径或调用AsyncFileUpload2.SaveAs()方法,但我并不期望uploadedcomplete事件被触发。文件是否在用户选择后立即上传?我认为您将上传到服务器和保存到服务器混合在一起。当用户在其PC上选择该文件时,该文件将立即发送到服务器。在UploadedComplete中,您可以决定mime类型是否正确,最后您可以将其物理保存在服务器上的某个位置。啊,这很有意义。我错误地假设控件只允许我在客户端选择一个文件,并且当我调用save方法时,上传和保存将同时完成,这显然不是这里的情况。谢谢你的帮助
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