You have not shared any issue with your code (does not work gives no clue to us on what issue you are facing). Looking at your code, I don't see where you define the
ContentType
of the binary data when you retrieve.
Refer the following article for it:
Convert Binary data to PDF file in C# and VB.Net[
^]
VB.NET code snippet:
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand()
cmd.CommandText = "SELECT Name, Data, ContentType FROM tblFiles WHERE Id=@Id"
cmd.Parameters.AddWithValue("@Id", id)
cmd.Connection = con
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
sdr.Read()
bytes = DirectCast(sdr("Data"), Byte())
contentType = sdr("ContentType").ToString()
fileName = sdr("Name").ToString()
End Using
con.Close()
End Using
End Using
Try out.