Use fileupload control and on submit button call below function and save the data in db in binary format. Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs) ' 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 tblFiles" _ & "(Name, ContentType, Data)" _ & " values (@Name, @ContentType, @Data)" Dim cmd As New SqlCommand(strQuery) 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
C
ChetanAhire
@ChetanAhire
Posts
-
How to upload excel sheet (.xlxs) to sql server 2012 with vb.net and asp.net -
Is there a control that displays a PDF and submits the pdf back to the server?Absolutely No, @Michael this is not possible as you need to code to save the PDF document in db. This can be done on submit button click.