如何使用VB.NET将新文件上传到FTP文件夹

时间:2015-01-19 06:17:26

标签: c# .net vb.net ftp

以下是用VB.NET编写的代码,用于将文件上传到FTP, 请注意我的路径ftp://ftp.myhost.in/Results/32071370264文件夹32071370264没有文件意味着尝试上传新文件

       Try      
            //source = "D:/files/afile.backup"
            Dim filename As String = Path.GetFileName(source)
            Dim ftpfullpath As String = "ftp://ftp.myhost.in/Results/32071370264"
            Dim ftp As FtpWebRequest = CType(FtpWebRequest.Create(ftpfullpath), FtpWebRequest)
            ftp.Credentials = New NetworkCredential("my_user_name", "password")

            ftp.KeepAlive = True
            ftp.UseBinary = True
            ftp.Method = WebRequestMethods.Ftp.UploadFile

            Dim fs As FileStream = File.OpenRead(source)
            Dim buffer As Byte() = New Byte(fs.Length - 1) {}
            fs.Read(buffer, 0, buffer.Length)
            fs.Close()

            Dim ftpstream As Stream = ftp.GetRequestStream //getting WebException here//
            ftpstream.Write(buffer, 0, buffer.Length)
            ftpstream.Close()
        Catch ex As Exception
            Throw ex
        End Try

我得到的例外是

  

远程服务器返回错误:(550)文件不可用(例如,   找不到文件,没有访问权限)

我试过这个answer,但错误是相同的

0 个答案:

没有答案