how to download binary file from SQL SERVER to VB.NET
Visual Basic
2
Posts
2
Posters
0
Views
1
Watching
-
-
Put this code in your page
Dim oAttachment As New GetAttach Try Me.Page.Response.Clear() Me.Page.Response.ClearContent() Me.Page.Response.ClearHeaders() Response.ContentType = "" Response.BinaryWrite(oAttachment.GetAttach()) Response.AddHeader("content-disposition", "attachment; filename=" + oAttachment.ATTACH\_NAME + "." + oAttachment.EXTENSION) Me.Page.Response.End() Catch ex As Exception throw ex End Try
GetAttach Class to call getter stored procedure
Public Function GetAttach(ByVal MyParameter As Integer)
Dim docFileReader As SqlDataReaderTry Dim Myadapter As New SqlDataAdapter Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString) Using command As New SqlCommand("GetAttachment", connection) command.CommandType = CommandType.StoredProcedure command.Parameters.Add(New SqlParameter("@MY\_SP\_PARAMETER", MyParameter)) connection.Open() docFileReader = command.ExecuteReader docFileReader.Read() EXTENSION = docFileReader.Item("EXTENSION").ToString ATTACH\_NAME = docFileReader.Item("ATTACH\_NAME").ToString WEIGHT = docFileReader.Item("WEIGHT").ToString Dim FileData(docFileReader.GetBytes(0, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte docFileReader.GetBytes(0, 0, FileData, 0, FileData.Length) docFileReader.Close() Return FileData End Using End Using Catch ex As Exception If IsNothing(docFileReader) = False Then docFileReader.Close() End If Throw ex End Try End Function