我有这个链接的工作代码,可以将文件上传到ftp站点:
' set up request...
Dim clsRequest As System.Net.FtpWebRequest = _
DirectCast(System.Net.WebRequest.Create("ftp://ftp.myserver.com/test.txt"), System.Net.FtpWebRequest)
clsRequest.Credentials = New System.Net.NetworkCredential("myusername", "mypassword")
clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
' read in file...
Dim bFile() As Byte = System.IO.File.ReadAllBytes("C:Temptest.txt")
' upload file...
Dim clsStream As System.IO.Stream = _
clsRequest.GetRequestStream()
clsStream.Write(bFile, 0, bFile.Length)
clsStream.Close()
clsStream.Dispose()
我想知道,如果ftp目录中已经存在该文件,该文件会被覆盖吗?
查看MSDN文档,它映射到FTP STOR命令。查看FTP STOR命令的定义,如果用户有权限,它将覆盖现有文件。
因此,在这种情况下,是的,文件将被覆盖。
是的,FTP协议在上传时覆盖现有文件。
请注意,有更好的方法来实现上传。
使用.NET框架将二进制文件上传到FTP服务器的最简单方法是使用WebClient.UploadFile
:
Dim client As WebClient = New WebClient
client.Credentials = New NetworkCredential("username", "password")
client.UploadFile("ftp://ftp.example.com/remote/path/file.zip", "C:localpathfile.zip")
如果您需要WebClient
不提供的更大控制(如TLS/SSL加密、ascii/text传输模式、活动模式、传输恢复等),请使用FtpWebRequest
。简单的方法是使用Stream.CopyTo
:将FileStream
复制到FTP流
Dim request As FtpWebRequest =
WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip")
request.Credentials = New NetworkCredential("username", "password")
request.Method = WebRequestMethods.Ftp.UploadFile
Using fileStream As Stream = File.OpenRead("C:localpathfile.zip"),
ftpStream As Stream = request.GetRequestStream()
fileStream.CopyTo(ftpStream)
End Using
如果你需要监控上传进度,你必须自己按块复制内容:
Dim request As FtpWebRequest =
WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip")
request.Credentials = New NetworkCredential("username", "password")
request.Method = WebRequestMethods.Ftp.UploadFile
Using fileStream As Stream = File.OpenRead("C:localpathfile.zip"),
ftpStream As Stream = request.GetRequestStream()
Dim read As Integer
Do
Dim buffer() As Byte = New Byte(10240) {}
read = fileStream.Read(buffer, 0, buffer.Length)
If read > 0 Then
ftpStream.Write(buffer, 0, read)
Console.WriteLine("Uploaded {0} bytes", fileStream.Position)
End If
Loop While read > 0
End Using
有关GUI进度(WinForms ProgressBar
),请参阅C#示例:
我们如何使用FtpWebRequest 显示上传进度条
如果要从文件夹上载所有文件,请参阅
中的C#示例使用WebClient将文件目录上载到FTP服务器。
来源:链接
STOR(STORE)
STOR-
此命令使FTP服务器接受通过数据连接传输的数据,并将数据作为文件存储在FTP服务器上。如果路径名中指定的文件存在于服务器站点,则其内容应替换为正在传输的数据。如果路径名中指定的文件不存在,则会在FTP服务器上创建一个新文件。
使用此函数上传文件
公用子上传文件(ByVal_FileName为字符串,ByVal_UploadPath为字符串,ByVal_FTPUser为字符串,ByVal_FTPPass为字符串)
Dim_FileInfo As New System.IO.FileInfo(_FileName)
' Create FtpWebRequest object from the Uri provided
Dim _FtpWebRequest As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(New Uri(_UploadPath)), System.Net.FtpWebRequest)
' Provide the WebPermission Credintials
_FtpWebRequest.Credentials = New System.Net.NetworkCredential(_FTPUser, _FTPPass)
' By default KeepAlive is true, where the control connection is not closed
' after a command is executed.
_FtpWebRequest.KeepAlive = False
' set timeout for 20 seconds
_FtpWebRequest.Timeout = 20000
' Specify the command to be executed.
_FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
' Specify the data transfer type.
_FtpWebRequest.UseBinary = True
' Notify the server about the size of the uploaded file
_FtpWebRequest.ContentLength = _FileInfo.Length
' The buffer size is set to 2kb
Dim buffLength As Integer = 2048
Dim buff(buffLength - 1) As Byte
' Opens a file stream (System.IO.FileStream) to read the file to be uploaded
Dim _FileStream As System.IO.FileStream = _FileInfo.OpenRead()
Try
' Stream to which the file to be upload is written
Dim _Stream As System.IO.Stream = _FtpWebRequest.GetRequestStream()
' Read from the file stream 2kb at a time
Dim contentLen As Integer = _FileStream.Read(buff, 0, buffLength)
' Till Stream content ends
Do While contentLen <> 0
' Write Content from the file stream to the FTP Upload Stream
_Stream.Write(buff, 0, contentLen)
contentLen = _FileStream.Read(buff, 0, buffLength)
Loop
' Close the file stream and the Request Stream
_Stream.Close()
_Stream.Dispose()
_FileStream.Close()
_FileStream.Dispose()
Catch ex As Exception
MessageBox.Show(ex.Message, "Upload Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
结束子
如何使用:
'使用FTP上载文件UploadFile("c:\UploadFile.doc","ftp://FTPHostName/UploadPath/UploadFile.doc","用户名","密码")
这是将文件上传到FTP服务器的工作代码
Dim request As FtpWebRequest = DirectCast(WebRequest.Create(New Uri("ftp://" & server & "/" & folderName & "/" & filename)), System.Net.FtpWebRequest)
request.Method = WebRequestMethods.Ftp.UploadFile
request.Credentials = New NetworkCredential(username, password)
request.UseBinary = True
request.UsePassive = True
Dim buffer(1023) As Byte
Dim bytesIn As Long = 1
Dim totalBytesIn As Long = 0
Dim filepath As System.IO.FileInfo = New System.IO.FileInfo(file)
Dim ftpstream As System.IO.FileStream = filepath.OpenRead()
Dim flLength As Long = ftpstream.Length
Dim reqfile As System.IO.Stream = request.GetRequestStream()
Do Until bytesIn < 1
bytesIn = ftpstream.Read(buffer, 0, 1024)
If bytesIn > 0 Then
reqfile.Write(buffer, 0, bytesIn)
totalBytesIn += bytesIn
End If
Loop
reqfile.Close()
ftpstream.Close()