使用 VB.NET 计算并限制上传的文件数量

Count and limit the number of files uploaded using VB.NET

我想从用户那里获取 5 张图像的输入,显示缩略图并将它们保存到 SQL- 数据库(图像 1、图像 2、图像 3、图像 4、图像 5)中的 5 列。

请为 sql 插入这 5 个文件添加代码。

请帮帮我

   Private Sub BrowseMultipleFilesButton_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles BrowseMultipleFilesButton.Click
    Dim OpenFileDialog1 As New OpenFileDialog
    OpenFileDialog1.Filter =
     "Images (*.BMP;*.JPG;*.GIF,*.PNG,*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF|" +
     "All files (*.*)|*.*"

    OpenFileDialog1.Multiselect = True
    Dim index As New Integer

    OpenFileDialog1.Title = "Select Photos"

    If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
        For Each file As String In OpenFileDialog1.FileNames
            Dim imageControl As New PictureBox()
            imageControl.Height = 100
            imageControl.Width = 100
            Dim myCallback As New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
            Dim myBitmap As New Bitmap(file)
            Dim myThumbnail As Image = myBitmap.GetThumbnailImage(96, 96, myCallback, IntPtr.Zero)
            imageControl.Image = myThumbnail

            PhotoGallary.Controls.Add(imageControl)
            index = OpenFileDialog1.FilterIndex()
        Next
        If index > 5 Then
            MessageBox.Show("Please select no more than 5 files")
        Else
            btn_Save.Enabled = True

        End If
    End If
End Sub

如果你只想允许5个图片文件,那么在测试文件数量之前就不需要费心制作缩略图了。由于 FileDialog.FileNames Property returns 是一个字符串数组,在测试 DialogResult 正常后,您要做的第一件事就是测试数组的长度:

If OpenFileDialog1.FileNames.Length > 5 Then 
    MessageBox.Show("Please select no more than 5 files")
    Exit sub
End If
'' The rest of your code, including the row btn_Save.Enabled = True.

问题已解决!

Private Sub BrowseMultipleFilesButton_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles BrowseMultipleFilesButton.Click
    Dim OpenFileDialog1 As New OpenFileDialog
    OpenFileDialog1.Filter =
     "Images (*.BMP;*.JPG;*.GIF,*.PNG,*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF|" +
     "All files (*.*)|*.*"

    OpenFileDialog1.Multiselect = True
    Dim index As New Integer

    OpenFileDialog1.Title = "Select Photos"

    If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
        If OpenFileDialog1.FileNames.Length > 5 Then
            MessageBox.Show("Please select no more than 5 files")
            Exit Sub
        End If
        If OpenFileDialog1.FileNames.Length < 5 Then
            MessageBox.Show("Please select 5 files")
            Exit Sub
        End If
        For Each file As String In OpenFileDialog1.FileNames
            Dim imageControl As New PictureBox()
            imageControl.Height = 100
            imageControl.Width = 100
            Dim myCallback As New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
            Dim myBitmap As New Bitmap(file)
            Dim myThumbnail As Image = myBitmap.GetThumbnailImage(96, 96, myCallback, IntPtr.Zero)
            imageControl.Image = myThumbnail

            PhotoGallary.Controls.Add(imageControl)
            index = OpenFileDialog1.FilterIndex()
        Next
        btn_Save.Enabled = True
        BrowseMultipleFilesButton.Enabled = False


    End If
End Sub