2011-10-07 62 views
0

我在我的頁面上有一個AsynchFileUpload,並且出於某種原因,當我嘗試使用它將文件保存到它所在的服務器時。AjaxControlToolkit Asynch File Uploader無法正常工作

控件將允許我在本地選擇一個文件,並在其文本框中顯示本地文件路徑,但是當我然後單擊我將用於提交所有詳細信息的頁面上的按鈕時,頁面一切都出錯了,我從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 

看來,文件名被某處遺失單擊該按鈕後,或只是從來沒有存儲

+0

看看我對類似問題的回答,看看它是否有幫助:http://stackoverflow.com/questions/5423911/cant-get-asyncfileupload-to-work-in-update-panel/5423992#5423992 –

+0

ks我以前見過一個,我覺得我對這個控件的工作原理有點困惑,因爲我認爲UploadedComplete事件在我實際上傳文件之前不會觸發,因爲我沒有指定文件路徑或稱爲AsyncFileUpload2.SaveAs()方法但我並不期待uploadcomplete事件被解僱。用戶選擇文件後立即上傳文件嗎? – Purplegoldfish

+0

我認爲你正在混合上傳到服務器並保存在服務器上。當用戶在他的PC上選擇了該文件時,它會立即發送到服務器。在UploadedComplete中,您可以決定MIME類型是正確的還是whatelse,並最終可以將其保存在服務器上的某個位置。 –

回答

0

解決這樣做:

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