Home » .NET 4.0 » Multiple file upload – VB.NET

Multiple file upload – VB.NET

Code behind only

Imports System.IO
Imports System.Web.UI
Imports System.Data.Sql
Imports System.Data.SqlClient

'define public classes/share them on page
Public ImgUpConf1 As String                   'displays img after success. upload
Public ImgUpConf2 As String                   ' "
Public BadImgFormat As String                 'passing banned file ext msg to page
Public NoImage As String = "no_image.jpg"     'default image no image jpg
Public NoImage2 As String = "no_image_2.jpg"  'no image jpg for img2, 3 and 4

'img directory - physical path
Dim saveDir As String = "\photos\"
Dim appPath As String = Request.PhysicalApplicationPath

If uID = "" And vaID = "" Then

MsgLbl.Text = "Your session expired. Please login again." Else 'img extensiong check Dim ImgExt1 As String = System.IO.Path.GetExtension(ImgUpload1.FileName) Dim ImgName1 = "photo1" & ImgExt1 Dim ImgExt2 As String = System.IO.Path.GetExtension(ImgUpload2.FileName) Dim ImgName2 = "photo2 & ImgExt2 'uploading first image If (ImgUpload1.HasFile) Then 'default file name 'Dim savePath As String = appPath + saveDir + Server.HtmlEncode(ImgUpload1.FileName) 'custom file name Dim savePath As String = Path.Combine(Server.MapPath("~/photos"), ImgName1) 'checking img type = only jpg and gif allowed If (ImgExt1.ToString = ".jpg") Or (ImgExt1.ToString = ".jpeg") + Or (ImgExt1.ToString = ".gif") Then ImgUpload1.SaveAs(savePath) ImgUpConf1 = ImgName1.ToString 'img photo and conf msg after sucess. upload FileUpd1.InnerHtml = "<table>" + <tr><td><img src='../photos/" & ImgUpConf1 & "' height='35' width='50'/>" + "<td style='vertical-align: top;'><strong>Image 1:" + " " & ImgUpload1.FileName & "<br />" + "<span class='lblAdded'>Image 1 successfuly uploaded!" Else 'if img <> jpg or <> gif msg BadImgFormat = ImgExt1.ToString ImgName1 = NoImage 'db photo record FileUpd1.InnerHtml = "<table>" + "<tr><td><img src='../photos/no_image.jpg' height='35' width='50'/>" + "<td style='vertical-align: top;' ><span style='color: red;'>" + "ERROR: The default image format is " & BadImgFormat & "!
" + "Only .jpg and .gif file type allowed.<br />" + "<a href='photo_up2.aspx?img=1' class='colorbox2'>Try again." + "" End If 'if img is not selected or not required Else ImgName1 = NoImage 'db photo record FileUpd1.InnerHtml = "<table>" + "<tr><td><img src='../photos/no_image.jpg' height='35' width='50'/>" + "<td style='vertical-align: top;' >The default image was not "+ "selected and uploaded.<br />" + "Default image photo will apply!" End If 'uploading second image If (ImgUpload2.HasFile) Then 'default file name 'Dim savePath As String = appPath + saveDir + Server.HtmlEncode(ImgUpload2.FileName) 'custom file name Dim savePath As String = Path.Combine(Server.MapPath("~/photos"), ImgName2) 'checking img type = only jpg and gif allowed If (ImgExt2.ToString = ".jpg") Or (ImgExt2.ToString = ".jpeg") + Or (ImgExt2.ToString = ".gif") Then ImgUpload2.SaveAs(savePath) ImgUpConf2 = ImgName2.ToString 'img photo and conf msg after sucess. upload FileUpd2.InnerHtml = "<table>" + <tr><td><img src='../photos/" & ImgUpConf1 & "' height='35' width='50'/>" + "<td style='vertical-align: top;'><strong>Image 2:" + " " & ImgUpload2.FileName & "<br />" + "<span class='lblAdded'>Image 2 successfuly uploaded!" + "" Else 'if img <> jpg or <> gif msg BadImgFormat = ImgExt1.ToString ImgName2 = NoImage 'db photo record FileUpd2.InnerHtml = "<table>" + "<tr><td><img src='../photos/no_image.jpg' height='35' width='50'/>" + "<td style='vertical-align: top;' ><span style='color: red;'>" + "ERROR: The default image format is " & BadImgFormat & "!
" + "Only .jpg and .gif file type allowed.<br />" + "<a href='photo_up2.aspx?img=2' class='colorbox2'>Try again." + "" End If 'if img is not selected or not required Else ImgName2 = NoImage 'db photo record FileUpd2.InnerHtml = "<table>" + "<tr><td><img src='../photos/no_image.jpg' height='35' width='50'/>" + "<td style='vertical-align: top;' >The default image was not " + "selected and uploaded."<br />" + "Default image photo will apply!" End If