473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Downloading a .doc stored in SQL Server from an ASP.net page

I am building a page that takes a single parameter: documentId. I have
documents stored in a SQL Server 2005 database. Storing and retrieving these
from a WinForms application works fine. No problemo there.

But, when retrieving the docs from my asp.net page, the docs are somehow
corrupted, and I cannot open them using MS Word.

Documents are stored in the "data" field of my Documents table. The "data"
fields is of datatype Image. The documentId parameter is used to select the
right document from the table.

Any ideas? Anything, anyone? Any help appreciated....

/j.jespersen
***************

<%@ Page Language="VB" %>

<script runat="server">

Public Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim con As New
System.Data.SqlClient.SqlConnection("server=myServ er;database=myDatabase;integrated
security=true")
Dim cmd As New System.Data.SqlClient.SqlCommand("Select * From
Documents where id=" & Request.QueryString("documentId"), con)

con.Open()
Dim dr As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader

If dr.Read Then
Response.Clear()
Response.AddHeader("Content-Type", "application/msword")
Response.AddHeader("Content-disposition", "attachment;
filename=myword.docx")
Response.BinaryWrite(CType(dr("data"), Byte()))
End If

con.Close()
con = Nothing

End Sub

</script>

***************
Mar 26 '07 #1
8 3655

"Jeppe Dige Jespersen" <jd*@jdj.dkwrote in message
news:uo**************@TK2MSFTNGP03.phx.gbl...
>I am building a page that takes a single parameter: documentId. I have
documents stored in a SQL Server 2005 database. Storing and retrieving
these from a WinForms application works fine. No problemo there.

But, when retrieving the docs from my asp.net page, the docs are somehow
corrupted, and I cannot open them using MS Word.

Documents are stored in the "data" field of my Documents table. The "data"
fields is of datatype Image. The documentId parameter is used to select
the right document from the table.

Any ideas? Anything, anyone? Any help appreciated....

/j.jespersen
***************

<%@ Page Language="VB" %>

<script runat="server">

Public Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim con As New
System.Data.SqlClient.SqlConnection("server=myServ er;database=myDatabase;integrated
security=true")
Dim cmd As New System.Data.SqlClient.SqlCommand("Select * From
Documents where id=" & Request.QueryString("documentId"), con)

con.Open()
Dim dr As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader

If dr.Read Then
Response.Clear()
Response.AddHeader("Content-Type", "application/msword")
Response.AddHeader("Content-disposition", "attachment;
filename=myword.docx")
Response.BinaryWrite(CType(dr("data"), Byte()))
End If

con.Close()
con = Nothing

End Sub

</script>

***************

Maybe this helps

Response.Buffer = True
Mar 26 '07 #2
>
Maybe this helps

Response.Buffer = True
Doesn't solve the problem, but thanks for trying :-)

/j.jespersen
Mar 26 '07 #3
On Mar 26, 1:24 pm, "Jeppe Dige Jespersen" <j...@jdj.dkwrote:
Maybe this helps
Response.Buffer = True

Doesn't solve the problem, but thanks for trying :-)

/j.jespersen
Try to save the response as a file to see how many bytes are there.
>From the first look your code is correct.
Mar 26 '07 #4
>
Try to save the response as a file to see how many bytes are there.
How would I do that? (i just dont know)

Could it be that the files that i am trying to save/retrieve are Word 2007
..docx files (that are really zip files)?
If I try it with a regular (Word 2003) document, it works fine. But not with
..docx files :-(
Any ideas?

/j.jespersen


Mar 26 '07 #5
On Mar 26, 1:46 pm, "Jeppe Dige Jespersen" <j...@jdj.dkwrote:
Could it be that the files that i am trying to save/retrieve are Word 2007
.docx files (that are really zip files)?
Indeed!

Try to remove

Response.AddHeader("Content-Type", "application/msword")

-------------------------

Response.Clear()
Response.AddHeader("Content-disposition", "attachment;
filename=myword.docx")
Response.BinaryWrite(CType(dr("data"), Byte()))

Mar 26 '07 #6
>
Response.AddHeader("Content-Type", "application/msword")
Same result. :-(

/j.jespersen
Mar 26 '07 #7
On Mar 26, 4:11 pm, "Jeppe Dige Jespersen" <j...@jdj.dkwrote:
Response.AddHeader("Content-Type", "application/msword")

Same result. :-(
Apologies, I've found an article in MDSN and didn't look what the code
they've suggested ;-)

http://msdn.microsoft.com/msdnmag/is...s/default.aspx

On the source code page you will find the following

Response.ContentEncoding = System.Text.Encoding.UTF8
Response.ContentType = "application/vnd.ms-word.document.12"

As you have no problem to open the file from WinForms. I think your
problem is in encoding, although maybe "vnd.ms-word.document.12" is
also important...

Please try

Mar 26 '07 #8
Here I've covered how to upload files into a SQL Server database and how to
successfully download them again to the client:
It works for Word docs and virtually any other kind of file too.
http://SteveOrr.net/articles/EasyUploads.aspx

--
I hope this helps,
Steve C. Orr,
MCSD, MVP, CSM, ASPInsider
http://SteveOrr.net
"Jeppe Dige Jespersen" <jd*@jdj.dkwrote in message
news:uo**************@TK2MSFTNGP03.phx.gbl...
>I am building a page that takes a single parameter: documentId. I have
documents stored in a SQL Server 2005 database. Storing and retrieving
these from a WinForms application works fine. No problemo there.

But, when retrieving the docs from my asp.net page, the docs are somehow
corrupted, and I cannot open them using MS Word.

Documents are stored in the "data" field of my Documents table. The "data"
fields is of datatype Image. The documentId parameter is used to select
the right document from the table.

Any ideas? Anything, anyone? Any help appreciated....

/j.jespersen
***************

<%@ Page Language="VB" %>

<script runat="server">

Public Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim con As New
System.Data.SqlClient.SqlConnection("server=myServ er;database=myDatabase;integrated
security=true")
Dim cmd As New System.Data.SqlClient.SqlCommand("Select * From
Documents where id=" & Request.QueryString("documentId"), con)

con.Open()
Dim dr As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader

If dr.Read Then
Response.Clear()
Response.AddHeader("Content-Type", "application/msword")
Response.AddHeader("Content-disposition", "attachment;
filename=myword.docx")
Response.BinaryWrite(CType(dr("data"), Byte()))
End If

con.Close()
con = Nothing

End Sub

</script>

***************

Mar 27 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: fibreiv | last post by:
I am trying to download files from my database that I uploaded to it. I can download bmp, txt file but have not been able to d/l pdf files. I am able to d/l pdf files stored in the file system...
6
by: Shawn | last post by:
Hi. How can I download a file and store it on the web server. I have a complete URL to the file, but I never know what kind of file it is. It can be pdf, jpg, tif, doc, xls etc. Thanks, Shawn
2
by: Bala | last post by:
Hi I am trying to download the PDF files from my webserver using ASP.Net. All my files are stored at F Drive on webserver. Like this F:\Main Folder\Sub Folder\Files\File1.pdf I am...
4
by: Joe | last post by:
I'm hosting my web service on a Windows 2003 box which is remotely located. When trying to add a web reference to a C# project I get an error message 'There was an error downloading...
1
by: just.starting | last post by:
Hi, My dot net client downloads files and checks for any new files time to time. The server is apache2.0.53 server. So what happens is that my file download thing works fine if I dont try to call...
3
by: just.starting | last post by:
Hi, My dot net client downloads files and checks for any new files time to time. The server is apache2.0.53 server. So what happens is that my file download thing works fine if I dont try to call...
14
by: suryadithya | last post by:
Hi, I am very new to php. And I have got to submit a project very soon (By tomorrow evening). So please help. The problem is this. I have created a file upload mechanism where the uploaded files...
4
by: Nik0001 | last post by:
Hello everyone! I have the following problem I need to download several HTML pages and get meta-tags out of the code. I decided it would be better to download only the meta-tags rather than...
12
by: Queez | last post by:
I have a .NET assembly (ScanControl.dll), which accesses a scanner on my client's local machine and allows them to scan a page (Scan()). I have an ASP.NET web application with a page which will...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.