将多个文件上传到vb.net中的本地文件夹

时间:2015-07-03 06:44:14

标签: vb.net file-upload vb.net-2010

我正在尝试创建一个允许用户将多个文件上传到本地文件夹的功能。

目前我只能上传一个文件。我需要一次上传更多文件。

我用于打开文件/文件夹

 a.Multiselect = True
    If a.ShowDialog() = Windows.Forms.DialogResult.OK Then
        removeatt.Show()
        removeatt.Text = "Remove Attachment"
        fpath.Text = a.FileName
        address.Text = System.IO.Path.GetFileName(a.FileName)
        Dim file As String
        file = fpath.Text.ToString
        Label7.Text = file
        If fpath.Text = "-" Then
            removeatt.Hide()
        Else
            removeatt.Show()
        End If
    End If

我用来保存附件的内容

If fpath.Text = "-" Then
        Else
            My.Computer.FileSystem.CopyFile(fpath.Text = "-", dir2 + Upload.Label16.Text, Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, Microsoft.VisualBasic.FileIO.UICancelOption.DoNothing)
        End If

感谢任何帮助

感谢

2 个答案:

答案 0 :(得分:1)

我不清楚你在哪里处理所选文件,第一个是关于删除附件而保存部分不是关于上传,而是将文件保存到用户的磁盘中。

一般情况下,我建议您编写一个一次处理一个文件的函数,这样就可以将函数与每个循环中要复制的文件列表一起提供。该函数有点“基本”来证明我的意思。

Runnable

答案 1 :(得分:0)

好了,看看MSDN中的示例,它有一个文件选择器,然后它将对象放在一个数组中,你可以循环并将它复制到你想要复制的地方。

Here是MSDN原创的 公共类Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    InitializeOpenFileDialog()
End Sub

Private Sub Selectfilebutton_Click_1(sender As Object, e As EventArgs) Handles Selectfilebutton.Click
    Dim dr As DialogResult = Me.OpenFileDialog1.ShowDialog()
    If (dr = System.Windows.Forms.DialogResult.OK) Then
        ' Read the files 
        Dim file As String
        For Each file In OpenFileDialog1.FileNames

            '' you can loop through the array of objects and use a function to do the copying 
            ' so for instance with my function it would be :
            ' copytodisk(Destination, file.filename)


            ' Create a PictureBox for each file, and add that file to the FlowLayoutPanel. 
            Try
                Dim pb As New PictureBox()
                Dim loadedImage As Image = Image.FromFile(file)
                pb.Height = loadedImage.Height
                pb.Width = loadedImage.Width
                pb.Image = loadedImage
                FlowLayoutPanel1.Controls.Add(pb)
            Catch SecEx As SecurityException
                ' The user lacks appropriate permissions to read files, discover paths, etc.
                MessageBox.Show("Security error. Please contact your administrator for details.\n\n" & _
                    "Error message: " & SecEx.Message & "\n\n" & _
                    "Details (send to Support):\n\n" & SecEx.StackTrace)
            Catch ex As Exception
                ' Could not load the image - probably permissions-related.
                MessageBox.Show(("Cannot display the image: " & file.Substring(file.LastIndexOf("\"c)) & _
                ". You may not have permission to read the file, or " + "it may be corrupt." _
                & ControlChars.Lf & ControlChars.Lf & "Reported error: " & ex.Message))
            End Try
        Next file
    End If
End Sub

Public Sub InitializeOpenFileDialog()
    ' Set the file dialog to filter for graphics files. 
    Me.OpenFileDialog1.Filter = _
            "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" + _
            "All files (*.*)|*.*"

    ' Allow the user to select multiple images. 
    Me.OpenFileDialog1.Multiselect = True
    Me.OpenFileDialog1.Title = "My Image Browser"
End Sub