我不是VB的经验丰富的程序员。
我正在创建一个桌面应用程序,用户可以通过ftp将某些文件上传到服务器。
这些文件都在本地的同一文件夹中,并上传到服务器上的同一文件夹。
我正在使用这段代码:
For Each File As String In My.Computer.FileSystem.GetFiles( _
path, _
FileIO.SearchOption.SearchAllSubDirectories, _
"*.jpg")
Dim fileInf As IO.FileInfo = New IO.FileInfo(File)
Dim uri As String = serveruri
Dim reqFTP As Net.FtpWebRequest
reqFTP = Net.FtpWebRequest.Create(New Uri(uri))
reqFTP.Credentials = New Net.NetworkCredential(user, pass)
reqFTP.KeepAlive = False
reqFTP.Method = Net.WebRequestMethods.Ftp.UploadFile
reqFTP.UseBinary = True
reqFTP.ContentLength = fileInf.Length
Dim buffLength As Integer = 2048
Dim buff(buffLength) As Byte
Dim contentLen As Integer
Dim fs As IO.FileStream = fileInf.OpenRead()
Dim strm As IO.Stream = reqFTP.GetRequestStream()
contentLen = fs.Read(buff, 0, buffLength)
While Not (contentLen = 0)
strm.Write(buff, 0, contentLen)
contentLen = fs.Read(buff, 0, buffLength)
End While
strm.Close()
fs.Close()
Next
使用此代码,我逐个上传文件,但总花费时间过长。
如何减少总花费时间?
我曾想过用线程做这件事或者异步上传文件但我找不到任何如何做的例子。
谢谢!
答案 0 :(得分:0)
我们使用此代码异步执行类似的工作:
w = New System.Net.WebClient
w.Credentials = New Net.NetworkCredential(username, Password)
w.UploadFileAsync(New System.Uri("ftp://ftp.yourdomain/yourfilename"), bupath)
答案 1 :(得分:0)
用户我的FTP课程
Public Class FTP
'-------------------------[BroCode]--------------------------
'----------------------------FTP-----------------------------
Private _credentials As System.Net.NetworkCredential
Sub New(ByVal _FTPUser As String, ByVal _FTPPass As String)
setCredentials(_FTPUser, _FTPPass)
End Sub
Public Sub UploadFile(ByVal _FileName As String, ByVal _UploadPath As String)
Dim _FileInfo As New System.IO.FileInfo(_FileName)
Dim _FtpWebRequest As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(New Uri(_UploadPath)), System.Net.FtpWebRequest)
_FtpWebRequest.Credentials = _credentials
_FtpWebRequest.KeepAlive = False
_FtpWebRequest.Timeout = 20000
_FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
_FtpWebRequest.UseBinary = True
_FtpWebRequest.ContentLength = _FileInfo.Length
Dim buffLength As Integer = 2048
Dim buff(buffLength - 1) As Byte
Dim _FileStream As System.IO.FileStream = _FileInfo.OpenRead()
Try
Dim _Stream As System.IO.Stream = _FtpWebRequest.GetRequestStream()
Dim contentLen As Integer = _FileStream.Read(buff, 0, buffLength)
Do While contentLen <> 0
_Stream.Write(buff, 0, contentLen)
contentLen = _FileStream.Read(buff, 0, buffLength)
Loop
_Stream.Close()
_Stream.Dispose()
_FileStream.Close()
_FileStream.Dispose()
Catch ex As Exception
MessageBox.Show(ex.Message, "Upload Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Public Sub DownloadFile(ByVal _FileName As String, ByVal _ftpDownloadPath As String)
Try
Dim _request As System.Net.FtpWebRequest = System.Net.WebRequest.Create(_ftpDownloadPath)
_request.KeepAlive = False
_request.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
_request.Credentials = _credentials
Dim _response As System.Net.FtpWebResponse = _request.GetResponse()
Dim responseStream As System.IO.Stream = _response.GetResponseStream()
Dim fs As New System.IO.FileStream(_FileName, System.IO.FileMode.Create)
responseStream.CopyTo(fs)
responseStream.Close()
_response.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Download Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Public Function GetDirectory(ByVal _ftpPath As String) As List(Of String)
Dim ret As New List(Of String)
Try
Dim _request As System.Net.FtpWebRequest = System.Net.WebRequest.Create(_ftpPath)
_request.KeepAlive = False
_request.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails
_request.Credentials = _credentials
Dim _response As System.Net.FtpWebResponse = _request.GetResponse()
Dim responseStream As System.IO.Stream = _response.GetResponseStream()
Dim _reader As System.IO.StreamReader = New System.IO.StreamReader(responseStream)
Dim FileData As String = _reader.ReadToEnd
Dim Lines() As String = FileData.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
For Each l As String In Lines
ret.Add(l)
Next
_reader.Close()
_response.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Directory Fetch Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Return ret
End Function
Private Sub setCredentials(ByVal _FTPUser As String, ByVal _FTPPass As String)
_credentials = New System.Net.NetworkCredential(_FTPUser, _FTPPass)
End Sub
End Class