作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我可以上传单个文件,现在如何将多个文件上传到 FTP 服务器:
这是我正在使用的代码:
私有(private)子上传文件(ByVal FTPAddress As String, ByVal filePath As String, ByVal username As String, ByVal password As String) '创建 FTP 请求
Try
Dim request As FtpWebRequest = DirectCast(FtpWebRequest.Create(FTPAddress & "/" & Path.GetFileName(filePath)), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.UploadFile
request.Credentials = New NetworkCredential(username, password)
request.UsePassive = True
request.UseBinary = True
request.KeepAlive = False
'Load the file
Dim stream As FileStream = File.OpenRead(filePath)
Dim buffer As Byte() = New Byte(CInt(stream.Length - 1)) {}
stream.Read(buffer, 0, buffer.Length)
stream.Close()
'Upload file
Dim reqStream As Stream = request.GetRequestStream()
reqStream.Write(buffer, 0, buffer.Length)
reqStream.Close()
MsgBox("Uploaded Successfully", MsgBoxStyle.Information)
Catch
MsgBox("Failed to upload.Please check the ftp settings", MsgBoxStyle.Critical)
End Try
End Sub
Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
btnUpload.Enabled = False
Application.DoEvents()
uploadFile(txtFTPAddress.Text, txtFilePath.Text, txtUsername.Text, txtPassword.Text)
btnUpload.Enabled = True
End Sub
这是我修改但不起作用的方法:
If Me.FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim f As New IO.DirectoryInfo(Me.FolderBrowserDialog1.SelectedPath)
For Each file As IO.FileInfo In f.GetFiles
Select Case file.Extension.ToLower
Case ".jpg", ".bmp", ".gif", ".png", ".ico"
CheckedListBox1.Items.Add(file.FullName, CheckState.Checked)
End Select
Next
For pix As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
btnUpload.Enabled = False
Application.DoEvents()
uploadFile(txtFTPAddress.Text, txtFilePath.Text, txtUsername.Text, txtPassword.Text)
btnUpload.Enabled = True
Next
End If
End Sub
最佳答案
For Each _____ in ______ collection
uploadFile(txtFTPAddress.Text, txtFilePath.Text, txtUsername.Text, txtPassword.Text)
Next
(填充空白取决于您用来存储文件名的控件。)
关于vb.net - 使用vb.net上传多个文件到FTP服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6713906/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!