【问题标题】:I need to create a text file and send it to an ftp server [closed]我需要创建一个文本文件并将其发送到 ftp 服务器 [关闭]
【发布时间】:2014-08-21 15:49:27
【问题描述】:

我目前正在开发一个程序,您可以在其中使用验证码登录帐户。我需要帮助将带有数字字符串的文件发送到我拥有的服务器。

例如:

发送“thisfile.txt”到ftp://myserver.com/thisfile.txt

我现在有

Imports System.IO.File
Public value = rnd.Next(1000, 9999)
Public vcode As Int32 = rndvalue
File.CreateText("vcode.vlo")

【问题讨论】:

    标签: .net vb.net ftp


    【解决方案1】:

    .Net 内置了一个FtpWebRequest class,您可以使用它通过 FTP 上传文件。

    MSDN 包含一个有用的示例How to: Upload Files with FTP,它是用 C# 编写的,但很容易转换为 VB.Net:

    Sub Main()
        ' Get the object used to communicate with the server.
        Dim request = CType(WebRequest.Create("ftp://www.contoso.com/test.htm"), FtpWebRequest)
        request.Method = WebRequestMethods.Ftp.UploadFile
    
        ' This example assumes the FTP site uses anonymous logon.
        request.Credentials = New NetworkCredential("anonymous", "janeDoe@contoso.com")
    
        ' Copy the contents of the file to the request stream.
        Dim sourceStream = New StreamReader("testfile.txt")
        Dim fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd())
        sourceStream.Close()
        request.ContentLength = fileContents.Length
    
        Dim requestStream = request.GetRequestStream()
        requestStream.Write(fileContents, 0, fileContents.Length)
        requestStream.Close()
    
        Dim response = CType(request.GetResponse(), FtpWebResponse)
    
        Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription)
    
        response.Close()
    End Sub
    

    【讨论】:

      【解决方案2】:

      这是一个上传任何文件到 ftp 服务器的函数

      Public Sub UploadFile(ByVal _FileName As String, ByVal _UploadPath As String, ByVal _FTPUser As String, ByVal _FTPPass As String)
      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
      End Sub
      

      代码使用示例

      UploadFile("C:/text.txt" , "ftp:example.com/file/text.txt" , "Ftpusername" , "Ftppassword")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-26
        • 1970-01-01
        • 1970-01-01
        • 2016-10-10
        • 2021-07-06
        • 2014-04-30
        • 1970-01-01
        • 2021-03-13
        相关资源
        最近更新 更多