With the FtpWebRequest and FtpWebResponse classes:
Private Sub Upload(ByVal source As String, ByVal target As String, _
ByVal credential As NetworkCredential)
Dim request As FtpWebRequest = _
DirectCast(WebRequest.Create(target), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.UploadFile
request.Credentials = credential
Dim reader As New FileStream(source, FileMode.Open)
Dim buffer(Convert.ToInt32(reader.Length - 1)) As Byte
reader.Read(buffer, 0, buffer.Length)
reader.Close()
request.ContentLength = buffer.Length
Dim stream As Stream = request.GetRequestStream
stream.Write(buffer, 0, buffer.Length)
stream.Close()
Dim response As FtpWebResponse = DirectCast(request.GetResponse, FtpWebResponse)
MessageBox.Show(response.StatusDescription, "File Uploaded")
response.Close()
End Sub
Dim credential As New NetworkCredential("username", "password")
Upload("test.zip", "ftp://myftp.net/test.zip%22, credential)
轉自: VB.NET
沒有留言:
張貼留言