473,394 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Opening MS WORD document from database

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.AppSetting s("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
Nov 19 '05 #1
2 2371
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.AppSetting s("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

Nov 19 '05 #2
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" <Ja******@discussions.microsoft.com> wrote in message
news:BA**********************************@microsof t.com...
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.AppSetting s("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

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sivaraman.S | last post by:
Issue: When I am opening a word document or a template from an ASP.NET Web page (language used VB.NET Visual Studio Version 2003) using Word.ApplicationClass object (Reference Word 11.0 object...
1
by: Andy Davis | last post by:
Hi I've written the following code for a button on a form to open a mail merge document in MS Word. The code opens the document fine but does not display the usual connection details to open the...
3
by: Andy Davis | last post by:
I have set up a mail merge document in Word 2003 which gets its data from my Access 2000 database. I want to set up a button on a form that: 1. runs the query to provide the dat for the merge...
1
by: Jose Egea | last post by:
Hello: I would like to make an application to manage word and pdf documents, opening and saving them in a sql database. Is there any way to open a document sending to the winword application a...
4
by: Yohancef Chin | last post by:
Hi, Being fairly new to .NET I am looking for a way to call MS Word from an event on a webform, and after the user is finished save that created document to an SQL Server database. Has anyone...
3
by: Yohancef Chin | last post by:
Hi, Being fairly new to .NET I am looking for a way to call MS Word from an event on a webform, and after the user is finished save that created document to an SQL Server database. Has anyone...
0
by: Sivaraman.S | last post by:
Issue : When I am trying to open a Word Document or a Template from a Web Application (ASP.NET Web page - Language Used VB.NET Visual Studio Version 2003) using Word.ApplicationClass (Refering...
1
by: ourspt | last post by:
Hi, We are working on an ASP.NET (VB.NET) application and we have a problem opening a word document from our application. When we try to open the word document, we do not get any error...
7
by: mkorch17 | last post by:
Hello, I have a server with a Word document and an Access database on it. I also have an ASP page running on the server. The Word document contains a mail merge with the Access database. What...
2
by: Colin Halliday | last post by:
I have a Word 2003 mail merge main document (form letter) that is linked to another Word document data source for the mail merge. If I open this doc using the Word GUI, it first asks me to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.