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