Ed
asked on
Iteration Help (Asp.net VB)
The below code allows me to save a file to the database using filestream. I want to allow multiple files to be uploaded. How do I iterate through this?
Protected Sub FileUp(ClientID As Integer, filename As String)
' Read the file and convert it to Byte Array
Dim filePath As String = fileUpload1.PostedFile.FileName
'Dim filename As String = Path.GetFileName(filePath)
Dim ext As String = Path.GetExtension(filename)
Dim contenttype As String = String.Empty
'Set the contenttype based on File Extension
Select Case ext
Case ".doc"
contenttype = "application/vnd.ms-word"
Exit Select
Case ".docx"
contenttype = "application/vnd.ms-word"
Exit Select
Case ".xls"
contenttype = "application/vnd.ms-excel"
Exit Select
Case ".xlsx"
contenttype = "application/vnd.ms-excel"
Exit Select
Case ".jpg"
contenttype = "image/jpg"
Exit Select
Case ".png"
contenttype = "image/png"
Exit Select
Case ".gif"
contenttype = "image/gif"
Exit Select
Case ".pdf"
contenttype = "application/pdf"
Exit Select
End Select
If contenttype <> String.Empty Then
Dim fs As Stream = fileUpload1.PostedFile.InputStream
Dim br As New BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(fs.Length)
'insert the file into database
Dim strQuery As String = "insert into [Qual].[QualSupFiles]" _
& "(QualSupID ,Name, ContentType, Data)" _
& " values (@QualSupID, @Name, @ContentType, @Data)"
Dim cmd As New SqlCommand(strQuery)
cmd.Parameters.Add("@QualSupID", SqlDbType.Int).Value = ClientID
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename
cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value _
= contenttype
cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes
InsertUpdateData(cmd)
lblMessage.ForeColor = System.Drawing.Color.Green
lblMessage.Text = "File Uploaded Successfully"
Else
lblMessage.ForeColor = System.Drawing.Color.Red
lblMessage.Text = "File format not recognised." _
& " Upload Image/Word/PDF/Excel formats"
End If
End Sub
If I select 3 files. It does 3 uploads but its the same file .Each of the File Upload control should only allowed a maximum of 1 file.
in order to allow 3 files to be uploaded, you need to call your Subroutine 3 times instead with a bit customization.
Protected Sub FileUp(upload As FileUpload, ClientID As Integer, filename As String)
' Read the file and convert it to Byte Array
Dim filePath As String = upload.PostedFile.FileName
...
ASKER
Thanks Ryan, could you give me a little more info...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect thanks
ASKER
Eg
If I select 3 files. It does 3 uploads but its the same file .
Open in new window