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

时间:2015-05-07 06:18:12

标签: .net vb.net visual-studio-2013 image-uploading sql-server-2014

我想从用户输入5个图像,显示缩略图并将它们保存到SQL数据库中的5列(image1,image2,image3,image4,image5)。

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

请帮帮我

   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

2 个答案:

答案 0 :(得分:0)

如果您只想要允许5个图像文件,则在测试文件数量之前无需费心缩略图。由于FileDialog.FileNames Property返回一个字符串数组,因此在测试DialogResult为Ok之后,您要做的第一件事就是测试数组的长度:

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.

答案 1 :(得分:0)

解决了问题!

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