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

Discussion in 'ASP .Net' started by Jeppe Dige Jespersen, Mar 26, 2007.

  1. 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>

    ***************
    Jeppe Dige Jespersen, Mar 26, 2007
    #1
    1. Advertising

  2. "Jeppe Dige Jespersen" <> wrote in message
    news:...
    >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
    Alexey Smirnov, Mar 26, 2007
    #2
    1. Advertising

  3. >
    > Maybe this helps
    >
    > Response.Buffer = True


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

    /j.jespersen
    Jeppe Dige Jespersen, Mar 26, 2007
    #3
  4. On Mar 26, 1:24 pm, "Jeppe Dige Jespersen" <> 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.
    Alexey Smirnov, Mar 26, 2007
    #4
  5. >
    > 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
    Jeppe Dige Jespersen, Mar 26, 2007
    #5
  6. On Mar 26, 1:46 pm, "Jeppe Dige Jespersen" <> 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()))
    Alexey Smirnov, Mar 26, 2007
    #6

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


    Same result. :-(

    /j.jespersen
    Jeppe Dige Jespersen, Mar 26, 2007
    #7
  8. On Mar 26, 4:11 pm, "Jeppe Dige Jespersen" <> 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/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
    Alexey Smirnov, Mar 26, 2007
    #8
  9. 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" <> wrote in message
    news:...
    >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>
    >
    > ***************
    >
    >
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider], Mar 27, 2007
    #9
  10. Jeppe Dige Jespersen

    ihtesham

    Joined:
    Jun 2, 2009
    Messages:
    2
    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
    ihtesham, Jun 2, 2009
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Wallace
    Replies:
    0
    Views:
    456
    John Wallace
    Jul 22, 2003
  2. =?Utf-8?B?cmljaGk=?=
    Replies:
    3
    Views:
    3,448
    =?Utf-8?B?cmljaGk=?=
    Nov 18, 2004
  3. =?Utf-8?B?SlA=?=
    Replies:
    1
    Views:
    7,283
    Steve C. Orr [MVP, MCSD]
    Aug 10, 2005
  4. Matt
    Replies:
    3
    Views:
    478
    Tor Iver Wilhelmsen
    Sep 17, 2004
  5. fibreiv

    Downloading Files stored in SQL Server

    fibreiv, Feb 10, 2005, in forum: ASP General
    Replies:
    1
    Views:
    105
Loading...

Share This Page