直接上传新的Ftp列表框行 [英] Direct Uploading New Ftp listbox Lines
问题描述
我可以将列表框行(上传/添加)到当前的Ftp列表框行服务器,而无需下载以前的ftp服务器列表框值.
Can i (Upload / Adding) Listbox Lines To Current Ftp listbox Lines Server Without download Previous ftp server Listbox value.
与其将其他列表框行上传到Ftp列表框行服务器!
Instead of Uploading addition listbox lines To Ftp Listbox Lines Server !
我可以简单地发送列表框新行,然后将其添加到当前列表框ftp服务器行中(不删除旧行,它仅添加新行) 使用当前的Ftp列表框行,而无需下载和上传整个ftp列表框?
Can I simply send listbox new lines and then it Added to currently listbox ftp server lines (without remove old lines, it only add new lines) With currently Ftp listbox Lines without me having to download and upload the whole ftp listbox?
示例:这是我的代码
下载代码(按钮1)[不重要]
Download Code (Button 1) [not important]
Dim request As FtpWebRequest =
WebRequest.Create("ftp://test.com/test.txt")
request.Method = WebRequestMethods.Ftp.DownloadFile
request.Credentials = New NetworkCredential("tester1",
"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
' Adding the listbox item's Before upload it again'
listbox1.Items.Add(".")
上传代码(按钮2)
Upload Code (Button 2)
[重要的是使其直接将新行上载到当前的ftp列表框 行服务器]
[important to make it direct upload new lines to currently ftp listbox lines server]
Dim request As FtpWebRequest =
WebRequest.Create("ftp://test.com/test.txt")
request.Method = WebRequestMethods.Ftp.UploadFile
request.Credentials = New NetworkCredential("tester1",
"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
Catch ex As Exception
致谢
推荐答案
注意:我看到您使用的是Visual Studio2012.某些代码可能不受支持(.NET版本为未标明).
如果是这样,请对此发表评论.
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
支持 FTP APPE
命令用于其 FtpWebRequest 化身.
WebRequest
supports the FTP APPE
command for its FtpWebRequest incarnation.
请参见 WebRequestMethods.Ftp → WebRequestMethods.Ftp.AppendFile .
此方法发送Ftp APPE
命令.如果上传的文件存在,它将附加新内容.
编写文本文件时,您可能想要附加
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.
由于具有ListBox
控件,因此可以提取其Items
文本,并通过以下方式在每个项目字符串值前添加换行符:
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)
这是一种经过修改的方法,该方法允许将一些文本行附加到FTP服务器上的现有文本文件中:
(请注意有关StreamWriter
的Encoding
的信息:它已设置为Default
→当前的本地代码页.
如果未指定编码,则默认为UTF8
.按要求修改).
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
如果需要Ssl
连接,请添加Imports
和以下行:将其粘贴到该方法的顶部:
( SecurityProtocolType 取决于您的连接要求)
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
这篇关于直接上传新的Ftp列表框行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!