Opening MS WORD document from database

Discussion in 'ASP .Net' started by Simon, Oct 27, 2005.

  1. Simon

    Simon Guest

    I'm simplying trying to download and and open word documents that are stored
    in the database. I figured out how to uploaded the documents as Byte, but I
    can't figure out how to get them back out properly. The following code is
    what I've tried (vb.NET)... It goes as far as asking me to Save/Open the
    file, but when I do, the contents of the documents is the webage that the
    download link was on! I'm fairly new to programming/web development, so i'm
    not totally sure what i'm doing. I hope i've included enough information.

    Thanks in advance!

    Simon

    --- VB.NET code ---

    Public Sub DownloadFile(ByVal File_Id As Integer)
    Dim dr As SqlDataReader
    Try
    Me.OpenConnection(ConfigurationSettings.AppSettings("MD_Runs_Connection_String"))

    Dim sqlCmd As New SqlCommand("usp_files_s_by_file_id", sqlConn)
    sqlCmd.CommandType = CommandType.StoredProcedure
    sqlCmd.Parameters.Add(New SqlParameter("@file_id", File_Id))

    dr = sqlCmd.ExecuteReader()

    Do While dr.Read
    Dim file() As Byte = CType(dr.Item("fileData"), Byte())
    memStream.Write(file, 0, file.Length)
    Loop

    memStream.Flush()
    ContentType = dr.Item("Application/octet-stream")

    Catch
    Throw
    Finally
    dr.Close()
    Me.CloseConnection()
    End Try
    End Sub
    Simon, Oct 27, 2005
    #1
    1. Advertising

  2. I'm a C# programmer...so bare with it.
    you would need to put that byte[] into the Response.OutputStream.
    so..
    Response.OutputStream.Write(byte[],0,the length);

    along with the Response.ContentType set correctly, the browser will be able
    to handle it as a word file.

    Response.OutputStream will essentially turn your aspx file into a doc
    file...so the wordopener.aspx when you browse it...it'll be treated as a doc
    file...so nothing in the html tags would matter, as you won't see them.

    and when you're finished...end with Response.End


    "Simon" wrote:

    > I'm simplying trying to download and and open word documents that are stored
    > in the database. I figured out how to uploaded the documents as Byte, but I
    > can't figure out how to get them back out properly. The following code is
    > what I've tried (vb.NET)... It goes as far as asking me to Save/Open the
    > file, but when I do, the contents of the documents is the webage that the
    > download link was on! I'm fairly new to programming/web development, so i'm
    > not totally sure what i'm doing. I hope i've included enough information.
    >
    > Thanks in advance!
    >
    > Simon
    >
    > --- VB.NET code ---
    >
    > Public Sub DownloadFile(ByVal File_Id As Integer)
    > Dim dr As SqlDataReader
    > Try
    > Me.OpenConnection(ConfigurationSettings.AppSettings("MD_Runs_Connection_String"))
    >
    > Dim sqlCmd As New SqlCommand("usp_files_s_by_file_id", sqlConn)
    > sqlCmd.CommandType = CommandType.StoredProcedure
    > sqlCmd.Parameters.Add(New SqlParameter("@file_id", File_Id))
    >
    > dr = sqlCmd.ExecuteReader()
    >
    > Do While dr.Read
    > Dim file() As Byte = CType(dr.Item("fileData"), Byte())
    > memStream.Write(file, 0, file.Length)
    > Loop
    >
    > memStream.Flush()
    > ContentType = dr.Item("Application/octet-stream")
    >
    > Catch
    > Throw
    > Finally
    > dr.Close()
    > Me.CloseConnection()
    > End Try
    > End Sub
    >
    >
    >
    =?Utf-8?B?SmFzb24gQ2h1?=, Oct 27, 2005
    #2
    1. Advertising

  3. Simon

    Simon Guest

    Thanks for the tips! I was about to try it out, but got a Configuration
    Error this morning, with "Interop.Microsoft.Office.Core" =( Thanks for
    your quick response though.

    Simon

    "Jason Chu" <> wrote in message
    news:...
    > I'm a C# programmer...so bare with it.
    > you would need to put that byte[] into the Response.OutputStream.
    > so..
    > Response.OutputStream.Write(byte[],0,the length);
    >
    > along with the Response.ContentType set correctly, the browser will be
    > able
    > to handle it as a word file.
    >
    > Response.OutputStream will essentially turn your aspx file into a doc
    > file...so the wordopener.aspx when you browse it...it'll be treated as a
    > doc
    > file...so nothing in the html tags would matter, as you won't see them.
    >
    > and when you're finished...end with Response.End
    >
    >
    > "Simon" wrote:
    >
    >> I'm simplying trying to download and and open word documents that are
    >> stored
    >> in the database. I figured out how to uploaded the documents as Byte,
    >> but I
    >> can't figure out how to get them back out properly. The following code
    >> is
    >> what I've tried (vb.NET)... It goes as far as asking me to Save/Open the
    >> file, but when I do, the contents of the documents is the webage that the
    >> download link was on! I'm fairly new to programming/web development, so
    >> i'm
    >> not totally sure what i'm doing. I hope i've included enough
    >> information.
    >>
    >> Thanks in advance!
    >>
    >> Simon
    >>
    >> --- VB.NET code ---
    >>
    >> Public Sub DownloadFile(ByVal File_Id As Integer)
    >> Dim dr As SqlDataReader
    >> Try
    >>
    >> Me.OpenConnection(ConfigurationSettings.AppSettings("MD_Runs_Connection_String"))
    >>
    >> Dim sqlCmd As New SqlCommand("usp_files_s_by_file_id",
    >> sqlConn)
    >> sqlCmd.CommandType = CommandType.StoredProcedure
    >> sqlCmd.Parameters.Add(New SqlParameter("@file_id", File_Id))
    >>
    >> dr = sqlCmd.ExecuteReader()
    >>
    >> Do While dr.Read
    >> Dim file() As Byte = CType(dr.Item("fileData"), Byte())
    >> memStream.Write(file, 0, file.Length)
    >> Loop
    >>
    >> memStream.Flush()
    >> ContentType = dr.Item("Application/octet-stream")
    >>
    >> Catch
    >> Throw
    >> Finally
    >> dr.Close()
    >> Me.CloseConnection()
    >> End Try
    >> End Sub
    >>
    >>
    >>
    Simon, Oct 28, 2005
    #3
    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. Charles A. Lackman

    Opening a Word Document

    Charles A. Lackman, Apr 21, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    2,178
    =?Utf-8?B?VGFyYXM=?=
    Apr 28, 2005
  2. =?Utf-8?B?U2FpZA==?=
    Replies:
    0
    Views:
    494
    =?Utf-8?B?U2FpZA==?=
    Nov 18, 2003
  3. Stephen Witter

    opening a word doc in word not browser

    Stephen Witter, May 18, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    487
    Stephen Witter
    May 18, 2004
  4. Simon
    Replies:
    3
    Views:
    569
    Daniel Walzenbach
    Nov 9, 2005
  5. Simon
    Replies:
    1
    Views:
    798
    Patrick.O.Ige
    Nov 9, 2005
Loading...

Share This Page