【问题标题】:Upload file contents from ListBox to FTP将文件内容从 ListBox 上传到 FTP
【发布时间】:2018-08-02 01:07:14
【问题描述】:

为了将文件内容读取到ListBox,我使用了上一个问题中的DownloadFile 方法
Loading file contents from FTP to ListBox

Dim request As FtpWebRequest = 
    WebRequest.Create("ftp://example.com/path/Ann.txt")
request.Method = WebRequestMethods.Ftp.DownloadFile
request.Credentials = New NetworkCredential("username", "password")

Using response As FtpWebResponse = request.GetResponse(),
      stream As Stream = response.GetResponseStream(),
      reader As StreamReader = New StreamReader(stream)
    While Not reader.EndOfStream
        ListBox1.Items.Add(reader.ReadLine())
    End While
End Using

现在我想添加另一个按钮,使用StreamWriterWebRequest.GetRequestStreamListBox 的内容上传回FTP。

【问题讨论】:

  • Thx,, 代码在stackoverflow.com/questions/48836591/… -=-= 有人告诉我再在这里提出一个新问题
  • Stack Overflow 很少接受“我该怎么做” 问题。您需要进行自己的研究和尝试。 -- 另外,真的需要接受the answer Martin gave you,因为您正在使用他的代码。要接受答案,请按 Martin 帖子左侧的勾选/复选标记,使其变为绿色,如下图所示:i.stack.imgur.com/LkiIZ.png -- 这是一个非常 重要的事情,因为它表示您对回答者的感激之情奖励他/她帮助您!
  • 谢谢,我想要做的,我是stackoverflow的新手,然后他问我接受答案,我没明白,谢谢你的重要信息你给了我
  • 确实为您提供了解释其工作原理的链接,尽管我个人认为以下是最好的。供参考,如果您想了解更多:How does accepting an answer work?
  • 让我问你这个:什么点击Button3时你要上传的具体是什么?

标签: .net vb.net ftp ftp-client ftpwebrequest


【解决方案1】:

你基本上需要我对
How to download file from FTP and upload it again的回答中的代码。


虽然很容易复制ListBox的内容(行),但您可以稍微修改它以使用StreamWriter逐行编写:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim request As FtpWebRequest =
        WebRequest.Create("ftp://example.com/path/Ann.txt")
    request.Method = WebRequestMethods.Ftp.UploadFile
    request.Credentials = New NetworkCredential("username", "password")
    request.UseBinary = False

    Using stream As Stream = request.GetRequestStream(),
          writer As StreamWriter = New StreamWriter(stream)
        For index As Integer = 0 To ListBox1.Items.Count - 1
            writer.WriteLine(ListBox1.Items(index))
        Next
    End Using
End Sub

【讨论】:

  • 在感谢之前,它工作得很好,,然后现在接受了问题,,你又快又乐于助人,最后一件事,,谢谢
猜你喜欢
  • 2018-07-27
  • 1970-01-01
  • 1970-01-01
  • 2012-07-06
  • 1970-01-01
  • 2013-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多