Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Downloading binary file out of SQL Server
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Downloading binary file out of SQL Server

 Post Reply Post Reply
Author
MrCarl View Drop Down
Newbie
Newbie


Joined: 29 March 2003
Status: Offline
Points: 29
Post Options Post Options   Thanks (0) Thanks(0)   Quote MrCarl Quote  Post ReplyReply Direct Link To This Post Topic: Downloading binary file out of SQL Server
    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
Back to Top
Diep-Vriezer View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 August 2003
Location: Netherlands
Status: Offline
Points: 831
Post Options Post Options   Thanks (0) Thanks(0)   Quote Diep-Vriezer Quote  Post ReplyReply Direct Link To This Post 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..
Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post 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)

Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post Posted: 10 February 2004 at 12:03pm
btw You need to Import the System.IO namespace for that solution...
Back to Top
MrCarl View Drop Down
Newbie
Newbie


Joined: 29 March 2003
Status: Offline
Points: 29
Post Options Post Options   Thanks (0) Thanks(0)   Quote MrCarl Quote  Post ReplyReply Direct Link To This Post 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 '%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

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.