2012-01-25 115 views
1

我有一個程序,當它們被拖到表單上時,它會將文件上傳到我們的服務器上,以便我們的客戶輕鬆地將大文件傳給我們。 我主要工作,但我想有一個進度條,以便用戶知道它正在工作,而不是讓它坐在那裏5分鐘,而文件在後檯安靜地上傳。用FtpWebRequest上傳進度條

我會很高興只是有進度條脈衝,所以它看起來程序工作,而不是凍結。如果我能顯示實際狀態,那會更好。

我的代碼:

Private Sub Grid1_Drop(sender As System.Object, e As System.Windows.DragEventArgs) Handles Grid1.Drop 
    Dim sFileInfo As System.IO.FileInfo 
    Dim sStatus As String = "" 

    If e.Data.GetDataPresent("FileDrop") Then 
     Try 
      Dim theFiles() As String = CType(e.Data.GetData("FileDrop", True), String()) 
      For Each file As String In theFiles 
       sFileInfo = New System.IO.FileInfo(file) 

       If UploadFile(txtUsername.Text, sFileInfo) Then 
        lstFileList.Items.Add(file & " - Uploaded") 
       Else 
        lstFileList.Items.Add(file & " - Upload Failed") 
       End If 
      Next 
     Catch ex As Exception 
      MsgBox(ex.Message) 
     End Try 
    End If 
End Sub 

Public Function UploadFile(ByVal User As String, ByVal oFile As FileInfo) As Boolean 
    Dim ftpRequest As FtpWebRequest 
    Dim ftpResponse As FtpWebResponse 
    Try 
     ftpRequest = CType(FtpWebRequest.Create(Base + User + "/" + oFile.Name), FtpWebRequest) 
     ftpRequest.Method = WebRequestMethods.Ftp.UploadFile 
     ftpRequest.Proxy = Nothing 
     ftpRequest.UseBinary = True 
     ftpRequest.Credentials = Cred 
     ftpRequest.KeepAlive = KeepAlive 
     ftpRequest.EnableSsl = UseSSL 
     If UseSSL Then ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate) 
     Dim fileContents(oFile.Length) As Byte 
     Using fr As FileStream = oFile.OpenRead 
      fr.Read(fileContents, 0, Convert.ToInt32(oFile.Length)) 
     End Using 
     Using writer As Stream = ftpRequest.GetRequestStream 
      writer.Write(fileContents, 0, fileContents.Length) 
     End Using 
     ftpResponse = CType(ftpRequest.GetResponse, FtpWebResponse) 
     ftpResponse.Close() 
     ftpRequest = Nothing 
     Return True 
    Catch ex As WebException 
     Return False 
    End Try 
End Function 

回答

2

看一看背景工人階級。 http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx這將釋放你的用戶界面,所以你可以添加一個進度條控件,讓它動畫,而你的文件正在上傳

+0

是的,我用過它之前,我想它可能會在這裏派上用場,但我不確定如何使FTPWebRequest報告已上傳多少文件。可能需要某種緩衝區,但我還沒找到很多好的例子。 – AndyD273

+0

你好我傾向於堅持選取框樣式或循環進度指示器,只是向用戶顯示實際發生的事情。 – emybob

+1

如果您正在上傳多個文件,您至少每個文件都要進行一次文件處理,並將其縮放爲all files = full bar。報告正在進行的文件的名稱和文件的大小。 – Paparazzi