问题描述
我可以(上传/添加)列表框行到当前 ftp 列表框行服务器而不下载以前的 ftp 服务器列表框值.
而不是将添加的列表框行上传到 Ftp 列表框行服务器!
我可以简单地发送列表框新行,然后将其添加到当前列表框 ftp 服务器行(不删除旧行,它只添加新行)使用当前的 ftp 列表框行,而无需我下载和上传整个 ftp 列表框?
示例:这是我的代码
<块引用>下载代码(按钮 1)[不重要]
Dim request As FtpWebRequest =WebRequest.Create("ftp://test.com/test.txt")request.Method = WebRequestMethods.Ftp.DownloadFilerequest.Credentials = New NetworkCredential("tester1",密码")使用响应作为 FtpWebResponse = request.GetResponse(),流作为 Stream = response.GetResponseStream(),阅读器作为 StreamReader = 新 StreamReader(stream)虽然不是 reader.EndOfStreamlistbox1.Items.Add(reader.ReadLine())结束时结束使用'添加列表框项目的在再次上传之前'listbox1.Items.Add(".")
<块引用>
上传代码(按钮 2)
[重要的是让它直接将新行上传到当前的 ftp 列表框线路服务器]
Dim request As FtpWebRequest =WebRequest.Create("ftp://test.com/test.txt")request.Method = WebRequestMethods.Ftp.UploadFilerequest.Credentials = New NetworkCredential("tester1",密码")request.UseBinary = False使用流作为 Stream = request.GetRequestStream(),作家作为 StreamWriter = 新 StreamWriter(流)对于索引 As Integer = 0 到 listbox1.Items.Count - 1writer.WriteLine(listbox1.Items(index))下一个结束使用抓住前任作为例外
问候
注意:我看到您使用的是 Visual Studio 2012.可能不支持某些代码(.Net 版本是未标明).如果是这种情况,请发表评论.
WebRequest
支持 FTP APPE
命令为其FtpWebRequest 化身.
请参阅 WebRequestMethods.Ftp → WebRequestMethods.Ftp.AppendFile.
此方法发送一个 Ftp APPE
命令.如果上传的文件存在,则会追加新的内容.
编写文本文件时,您可能需要附加 Environment.Newline 序列到每一行,如果/当需要.
由于您有一个 ListBox
控件,因此您可以提取其 Items
文本并以这种方式为每个项目字符串值添加换行符:
Dim TextLines As String() = listBox1.Items.Cast(Of String)().Select(Function(ln) Environment.NewLine + ln).ToArray()
你可以这样调用下面的方法:
Dim 结果 As Long = Await FtpAppenAsync("ftp://ftp.server.com/[EntryDir]/[ExistingFile]", TextLines)
这是一种修改后的方法,它允许将一些文本行附加到 FTP 服务器上的现有文本文件中:
(注意 StreamWriter
的 Encoding
:它设置为 Default
→ 当前本地代码页.
未指定编码时,默认为 UTF8
.根据需要修改).
公共异步函数 FtpAppenAsync(ResourceName As String, TextData As String()) As Task(Of Integer)昏暗请求 As FtpWebRequest = CType(WebRequest.Create(ResourceName), FtpWebRequest)request.Credentials = New NetworkCredential("[FtpAccount]", "[FtpPassword]")request.Method = WebRequestMethods.Ftp.AppendFilerequest.UseBinary = Truerequest.UsePassive = TrueDim TextLinesWritten As Integer = 0尝试使用 ftpStream 作为 Stream = Await request.GetRequestStreamAsync()使用 ftpWriter 作为新的 StreamWriter(ftpStream, Encoding.Default)对于每个 TextLine 作为 TextData 中的字符串等待 ftpWriter.WriteAsync(TextLine)TextLinesWritten += 1Console.WriteLine("已上传 {0} 行", TextLinesWritten)下一个结束使用结束使用使用响应作为 FtpWebResponse = CType(Await request.GetResponseAsync(), FtpWebResponse)'Log-返回上传失败的StatusCode如果不是 (response.StatusCode = FtpStatusCode.ClosingData) 则返回 -1结束使用抓住前任作为例外'日志/报告前TextLinesWritten = -1扔结束尝试返回 TextLinesWritten结束功能
如果需要 Ssl
连接,请添加 Imports
和这些行:将它们粘贴到该方法的顶部:
(SecurityProtocolType取决于您的连接要求)
导入 System.Net.Security导入 System.Security.Cryptography.X509Certificates'方法代码ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12ServicePointManager.ServerCertificateValidationCallback =新 RemoteCertificateValidationCallback(Function(s, Cert, Chain, sslErrors)返回真结束函数)request.EnableSsl = True
Can i (Upload / Adding) Listbox Lines To Current Ftp listbox Lines Server Without download Previous ftp server Listbox value.
Instead of Uploading addition listbox lines To Ftp Listbox Lines Server !
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?
Example : That's My Code
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(".")
Upload Code (Button 2)
[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
Regards
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.Ftp → WebRequestMethods.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
这篇关于直接上传新的 Ftp 列表框行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!