Print Page | Close Window

Downloading binary file out of SQL Server

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=9693
Printed Date: 29 March 2026 at 5:17am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Downloading binary file out of SQL Server
Posted By: MrCarl
Subject: Downloading binary file out of SQL Server
Date Posted: 10 February 2004 at 6:27am
I have got a script working to upload files (such as word docs) into a sql server database, but im struggling on creating a asp.net page to download them...

Im using this at the moment:


<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">

Sub Page_Load(sender as Object, e as EventArgs)

Dim conSql as New SqlConnection(ConfigurationSettings.AppSettings("strCon"))
Dim strSql As String = "SELECT * FROM tblFileUpload WHERE idFileUpload = 10;"
Dim oCmd As New SqlCommand(strSql, conSql)

conSql.Open()
Dim dr As SqlDataReader = oCmd.ExecuteReader()

dr.Read()

Response.ContentType = dr.Item("strContentType")
Response.BinaryWrite(dr.Item("binAttachment"))

conSql.Close()

End Sub

</script>


But it outputs all the binary data into the browser window rather than letting you download the file... Can anyone help??

Thanks

- Carl S



Replies:
Posted By: Diep-Vriezer
Date Posted: 10 February 2004 at 8:58am
If you want this, you'll have to make the script create a new file (*.bin), and write the data to it. Then Response.Redirect to the .bin file, and the client app will download it (usually).

-------------
Gone..


Posted By: Mart
Date Posted: 10 February 2004 at 12:01pm

Or

Response.ContentType = "application/octet-stream"

Dim br As New BinaryReader(File.OpenRead("C:\whatever.txt"))

Dim bytes As Byte() = br.ReadBytes(br.BaseStream.Length)

br.Close

Response.BinaryWrite(bytes)



Posted By: Mart
Date Posted: 10 February 2004 at 12:03pm
btw You need to Import the System.IO namespace for that solution...


Posted By: MrCarl
Date Posted: 12 February 2004 at 4:39am

Well im really having no luck :( everything I do just results in the data being shown in the web browser as lots of mailto:'%hjg&456&*@76g' - ' mailto:'%hjg&456&*@76g - %hjg&456&*@76g ' and it doesnt actually give you the option to download the word file.

I have tried both ways as u will see by the commented bit in my code as well, I was going to try what Diep suggested as a short term solution. But does anyone know how you save the binary file out of the database ie dr.Item("binAttachment") in my case as a file on the server file system??

My code at the moment is like this:

<%@ Page Language="VB" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.IO" %>
<script runat="server">

Sub Page_Load(sender as Object, e as EventArgs)

 Dim conSql as New SqlConnection(ConfigurationSettings.AppSettings("strCon"))
 Dim strSql As String = "SELECT * FROM tblFileUpload WHERE idFileUpload = 10;"
 Dim oCmd As New SqlCommand(strSql, conSql)

 conSql.Open()
 Dim dr As SqlDataReader = oCmd.ExecuteReader()

 dr.Read()

 Response.Buffer = True
 Response.Clear
 Response.AddHeader("Content-Disposition", "attachment; filename=test.doc")
 Response.AddHeader("Content-Length", dr.Item("intSize"))
 Response.ContentType = dr.Item("strContentType")
 Response.BinaryWrite(dr.Item("binAttachment"))
 
 'Response.ContentType = "application/octet-stream"
 'Dim br As New BinaryReader(dr.Item("binAttachment"))
 'Dim bytes As Byte() = br.ReadBytes(br.BaseStream.Length)
 'br.Close
 'Response.BinaryWrite(bytes)
 
 conSql.Close()
  
End Sub

</script>

Thanks

Carl S




Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net