Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   Downloading a .doc stored in SQL Server from an ASP.net page (http://www.velocityreviews.com/forums/t487216-downloading-a-doc-stored-in-sql-server-from-an-asp-net-page.html)

Jeppe Dige Jespersen 03-26-2007 10:28 AM

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>

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



Alexey Smirnov 03-26-2007 11:04 AM

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

"Jeppe Dige Jespersen" <jdj@jdj.dk> wrote in message
news:uofsuF5bHHA.3408@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



Jeppe Dige Jespersen 03-26-2007 11:24 AM

Re: Downloading a .doc stored in SQL Server from an ASP.net page
 
>
> Maybe this helps
>
> Response.Buffer = True


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

/j.jespersen



Alexey Smirnov 03-26-2007 11:36 AM

Re: Downloading a .doc stored in SQL Server from an ASP.net page
 
On Mar 26, 1:24 pm, "Jeppe Dige Jespersen" <j...@jdj.dk> 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.



Jeppe Dige Jespersen 03-26-2007 11:46 AM

Re: Downloading a .doc stored in SQL Server from an ASP.net page
 
>
> 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





Alexey Smirnov 03-26-2007 12:38 PM

Re: Downloading a .doc stored in SQL Server from an ASP.net page
 
On Mar 26, 1:46 pm, "Jeppe Dige Jespersen" <j...@jdj.dk> 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()))


Jeppe Dige Jespersen 03-26-2007 02:11 PM

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

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


Same result. :-(

/j.jespersen



Alexey Smirnov 03-26-2007 02:26 PM

Re: Downloading a .doc stored in SQL Server from an ASP.net page
 
On Mar 26, 4:11 pm, "Jeppe Dige Jespersen" <j...@jdj.dk> 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


Steve C. Orr [MCSD, MVP, CSM, ASP Insider] 03-27-2007 03:58 AM

Re: Downloading a .doc stored in SQL Server from an ASP.net page
 
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" <jdj@jdj.dk> wrote in message
news:uofsuF5bHHA.3408@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>
>
> ***************
>
>



ihtesham 06-02-2009 03:15 PM

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


All times are GMT. The time now is 08:48 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.