【发布时间】:2016-08-07 10:22:04
【问题描述】:
我正在使用此代码:
Imports System.IO
Imports System.Net
Public Class sendftp
Public Function UploadFileToFtp(ByVal f As String, ByVal host As String, ByVal username As String, ByVal password As String, ByVal folderToUploadTo As String) As Boolean
Try
'create an FtpRequest object, holds the value of the path you are trying to reach
Dim ftpRequest As FtpWebRequest = DirectCast(FtpWebRequest.Create(New Uri(host & "/" & Path.GetFileName(folderToUploadTo))), FtpWebRequest)
'pass in our login credentials
ftpRequest.Credentials = New NetworkCredential(username, password)
'set the method to Upload, since we're uploading a file
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile
'don't use passive mode
ftpRequest.UsePassive = True
'unless we're uploading a file like an image
'we need to set this to false as we're not uploading
'binary data
ftpRequest.UseBinary = True
'set KeepAlive to false
ftpRequest.KeepAlive = False
'now create a new FileStream, then open the file we're uploading
'this allows us to get the size of the file for our buffer
Dim stream As FileStream = File.OpenRead(f)
'create a byte[] array buffer the size of the file
'we're uploading
Dim buffer As Byte() = New Byte(stream.Length - 1) {}
'read in our file into the FileStream
stream.Read(buffer, 0, buffer.Length)
'close the FileStream
stream.Close()
'create a new stream, this will be used to write to the
'FTP server
Dim requestStream As Stream = ftpRequest.GetRequestStream()
'write the data to the FTP server
requestStream.Write(buffer, 0, buffer.Length)
'close the stream
requestStream.Close()
'since we made it this far return true
Return True
Catch ex As Exception
'something went wront so let the user know
MessageBox.Show("Error uploading file: " + ex.Message, "Upload Error")
'return false
Return False
End Try
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
UploadFileToFtp("C:\file.txt", "ftp://ftp.drivehq.com", "**user**", "**password**", "new.txt")
End Sub
End Class
而且它有效。
但是我想使用我的 FileZilla 服务器,所以我编辑了 ftp url:
UploadFileToFtp("C:\file.txt", "ftp://localhost", "**user**", "**password**", "new.txt")
但我收到此错误:?
远程服务器返回错误:(550) 文件不可用(例如,找不到文件,无法访问)。
我想不通,有什么想法吗?
【问题讨论】:
标签: vb.net