Private Sub myUpload(ByVal fileName As String, ByVal uploadUrl As String)
Dim requestStream As Stream = Nothing
Dim fileStream As FileStream = Nothing
Dim uploadResponse As FtpWebResponse = Nothing
Try
Dim uploadRequest As FtpWebRequest = WebRequest.Create(uploadUrl)
uploadRequest.Method = WebRequestMethods.Ftp.UploadFileWithUniqueName
' UploadFile is not supported through an Http proxy
' so we disable the proxy for this request.
uploadRequest.Proxy = Nothing
requestStream = uploadRequest.GetRequestStream()
fileStream = File.Open(fileName, FileMode.Open)
Dim buffer(1024) As Byte
Dim bytesRead As Integer
While True
bytesRead = fileStream.Read(buffer, 0, buffer.Length)
If bytesRead = 0 Then
Exit While
End If
requestStream.Write(buffer, 0, bytesRead)
End While
' The request stream must be closed before getting the response.
requestStream.Close()
uploadResponse = uploadRequest.GetResponse()
lblMsg.Text = "Upload complete."
Catch ex As UriFormatException
lblMsg.Text = ex.Message
Catch ex As IOException
lblMsg.Text = ex.Message
Catch ex As WebException
lblMsg.Text = ex.Message
Finally
If uploadResponse IsNot Nothing Then
uploadResponse.Close()
End If
If fileStream IsNot Nothing Then
fileStream.Close()
End If
If requestStream IsNot Nothing Then
requestStream.Close()
End If
End Try
End Sub
Protected Sub btnFtp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFtp.Click
myUpload("C:\a.txt", "ftp://192.168.5.1/ch101/")
End Sub
这段代码只能商船服务器端的文件.