Direct Uploading New Ftp listbox Lines

▼魔方 西西 提交于 2019-12-02 10:15:30

Note: I see you're using Visual Studio 2012. There's the chance that some of the code might not be supported (the .Net version is not specified). Comment about it if that's the case.

WebRequest supports the FTP APPE command for its FtpWebRequest incarnation.

See the WebRequestMethods.FtpWebRequestMethods.Ftp.AppendFile.

This method sends an Ftp APPE command. If the uploaded file exists, it will append the new content.
When writing a text file, you may want to append an Environment.Newline sequence to each line, if/when need.

Since you have a ListBox control, you can extract its Items text and prepend a line feed to each item string value this way:

Dim TextLines As String() = listBox1.Items.Cast(Of String)().Select(Function(ln) Environment.NewLine + ln).ToArray()

You can call the method that follows this way:

Dim result As Long = Await FtpAppenAsync("ftp://ftp.server.com/[EntryDir]/[ExistingFile]", TextLines)

Here's a modified method that allows to append some text lines to an existing text file on an FTP Server:
(Take note about the Encoding of the StreamWriter: it's set to Default → current Local Codepage.
When an Encoding is not specified, it defaults to UTF8. Modify as required
).

Public Async Function FtpAppenAsync(ResourceName As String, TextData As String()) As Task(Of Integer)

    Dim request As FtpWebRequest = CType(WebRequest.Create(ResourceName), FtpWebRequest)
    request.Credentials = New NetworkCredential("[FtpAccount]", "[FtpPassword]")
    request.Method = WebRequestMethods.Ftp.AppendFile
    request.UseBinary = True
    request.UsePassive = True

    Dim TextLinesWritten As Integer = 0
    Try
        Using ftpStream As Stream = Await request.GetRequestStreamAsync()
            Using ftpWriter As New StreamWriter(ftpStream, Encoding.Default)
                For Each TextLine As String In TextData
                    Await ftpWriter.WriteAsync(TextLine)
                    TextLinesWritten += 1
                    Console.WriteLine("Uploaded {0} lines", TextLinesWritten)
                Next
            End Using
        End Using
        Using response As FtpWebResponse = CType(Await request.GetResponseAsync(), FtpWebResponse)
            'Log-Return the StatusCode of the failed upload
            If Not (response.StatusCode = FtpStatusCode.ClosingData) Then Return -1
        End Using
    Catch ex As Exception
        'Log/report ex
        TextLinesWritten = -1
        Throw
    End Try
    Return TextLinesWritten
End Function

If an Ssl connection is required, add the Imports and these lines: paste them on top of that method:
(The SecurityProtocolType depends on your connection requirements)

Imports System.Net.Security
Imports System.Security.Cryptography.X509Certificates

'Method code
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

ServicePointManager.ServerCertificateValidationCallback =
    New RemoteCertificateValidationCallback(Function(s, Cert, Chain, sslErrors)
                                                Return True
                                            End Function)
request.EnableSsl = True
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!