Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Downloading a .doc stored in SQL Server from an ASP.net page

Reply
Thread Tools

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

 
 
Jeppe Dige Jespersen
Guest
Posts: n/a
 
      03-26-2007
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>

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


 
Reply With Quote
 
 
 
 
Alexey Smirnov
Guest
Posts: n/a
 
      03-26-2007

"Jeppe Dige Jespersen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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


 
Reply With Quote
 
 
 
 
Jeppe Dige Jespersen
Guest
Posts: n/a
 
      03-26-2007
>
> Maybe this helps
>
> Response.Buffer = True


Doesn't solve the problem, but thanks for trying

/j.jespersen


 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      03-26-2007
On Mar 26, 1:24 pm, "Jeppe Dige Jespersen" <(E-Mail Removed)> wrote:
> > 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.


 
Reply With Quote
 
Jeppe Dige Jespersen
Guest
Posts: n/a
 
      03-26-2007
>
> 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




 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      03-26-2007
On Mar 26, 1:46 pm, "Jeppe Dige Jespersen" <(E-Mail Removed)> wrote:
> 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()))

 
Reply With Quote
 
Jeppe Dige Jespersen
Guest
Posts: n/a
 
      03-26-2007

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


Same result.

/j.jespersen


 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      03-26-2007
On Mar 26, 4:11 pm, "Jeppe Dige Jespersen" <(E-Mail Removed)> wrote:
> > 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

 
Reply With Quote
 
Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
Guest
Posts: n/a
 
      03-27-2007
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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>
>
> ***************
>
>


 
Reply With Quote
 
ihtesham ihtesham is offline
Junior Member
Join Date: Jun 2009
Posts: 2
 
      06-02-2009
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help. Getting a An error has occurred while establishing a connectionto the server. When connecting to SQL Server 2005, this failure may be causedby the fact that under the default settings SQL Server does not allow remote aboutjav.com@gmail.com ASP .Net 0 05-03-2008 12:43 PM
Uploading downloading Image to SQL Server using ASP.net and VB.Net =?Utf-8?B?SlA=?= ASP .Net 1 08-10-2005 06:44 PM
Downloading Files stored in SQL Server fibreiv ASP General 1 02-10-2005 08:02 PM
How to read an SQL Server into a ASP page and then change, add, delete and write it back to SQL Server Belinda ASP General 4 06-11-2004 12:16 PM
Downloading files stored in image column of database... Charlie@CBFC ASP .Net 0 01-07-2004 07:43 PM



Advertisments