使用VB.NET上传多个文件到FTP服务器

4

我可以上传单个文件,现在我该如何上传多个文件到FTP服务器:

这是我正在使用的代码:

私有子上传文件(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
2个回答

4
For Each _____ in ______ collection
uploadFile(txtFTPAddress.Text, txtFilePath.Text, txtUsername.Text, txtPassword.Text)
Next

(根据您用于存储文件名的控件填写空白。)

每次上传时,我都会收到文件已上传的消息,如果没有上传成功,我会收到异常消息,请问如何修改代码,以便在所有图像上传完成后显示“已上传”,如果未完成,则应显示“上传失败”的消息。 - user676589
有几种方法可以做到这一点,但你可以尝试的一种方法是将你的for-each循环放在try-catch块内。在Catch部分,你可以决定是否退出循环并显示错误消息,或者只是跳过该文件并记录错误,或者任何你认为最合适的方式。至于成功消息,我会把它删除掉,或者放在try-catch块的Finally部分。 - Chains
是的,我以前做过,但这个过程太长了。但是,如果我不把for each循环放在try catch块内,上传所有内容只需要几秒钟。 - user676589

3
根据您获取文件列表的方式,只需对集合进行迭代即可。我举一个字符串的例子:
Dim files As List(Of String) = New List(Of String)

For Each file In files
  uploadFile(txtFTPAddress.Text, file, txtUsername.Text, txtPassword.Text)
Next

此外,对于实现了 IDisposable 接口的对象,考虑使用 using 语句。
 Private Sub uploadFile(ByVal FTPAddress As String, ByVal filePath As String, ByVal username As String, ByVal password As String) 'Create FTP request

        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

            Dim buffer As Byte() = Nothing
            'Load the file
            Using stream As FileStream = File.OpenRead(filePath)
                buffer = New Byte(CInt(stream.Length - 1)) {}
                stream.Read(buffer, 0, buffer.Length)
            End Using

            'Upload file
            Using reqStream As Stream = request.GetRequestStream()
                reqStream.Write(buffer, 0, buffer.Length)
            End Using

            MsgBox("Uploaded Successfully", MsgBoxStyle.Information)
        Catch
            MsgBox("Failed to upload.Please check the ftp settings", MsgBoxStyle.Critical)
        End Try
    End Sub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接