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

  • Thread starter Jeppe Dige Jespersen
  • Start date
J

Jeppe Dige Jespersen

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=myServer;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>

***************
 
G

Guest

Jeppe Dige Jespersen said:
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=myServer;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
 
J

Jeppe Dige Jespersen

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
 
G

Guest

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()))
 
G

Guest

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/issues/06/11/BasicInstincts/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
 
Joined
Jun 2, 2009
Messages
2
Reaction score
0
Corrupted/Unreadable document retrieved from database

Hi guyz i finally figured it out.

When you save a document into the database, the contenttype of the posted file is 'Application/msword'. When retreiving it back from the database specity the content type as 'Application/msword' (same when uploading).

Your document won't be corrupted with ??? and unreadable characters
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top