Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   RE: Opening MS WORD document from database (http://www.velocityreviews.com/forums/t112886-re-opening-ms-word-document-from-database.html)

Simon 11-04-2005 08:55 PM

RE: Opening MS WORD document from database
 
I tried putting it into a response.outputstream, but still not getting the
actual file. Now, it displays the source of the web page instead. What am
I doing wrong?

----[VB.net code]-----
Dim db As New MD_Runs.Database(strDBConn)
Dim dt As DataTable
Dim sqlParams() As SqlParameter = { _
New SqlParameter("@rid", rid)}

dt = db.FillDataTable("usp_files_s_one", sqlParams)

Dim fileData() As Byte = CType(dt.Rows(0)("FileData"), Byte())

Response.Clear()
Response.ClearHeaders()
Response.ClearContent()
Response.ContentType = "Application/msword"
Response.OutputStream.Write(fileData, 0, fileData.Length)
Response.OutputStream.Flush()
Response.OutputStream.Close()
----------------------


> "Jason Chu" <JasonChu@discussions.microsoft.com> wrote in message
> news:BA02D945-1DF4-4EA0-B940-0BD2E9F4CB17@microsoft.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
>>>
>>>
>>>

>
>




Daniel Walzenbach 11-05-2005 02:34 AM

Re: Opening MS WORD document from database
 
Simon,

make sure to delete everything except the page directive from your .aspx
page. That should do the trick!

Regards

Daniel


"Simon" <sbee@leupold.com> schrieb im Newsbeitrag
news:%23YxeiIY4FHA.700@TK2MSFTNGP15.phx.gbl...
>I tried putting it into a response.outputstream, but still not getting the
>actual file. Now, it displays the source of the web page instead. What am
>I doing wrong?
>
> ----[VB.net code]-----
> Dim db As New MD_Runs.Database(strDBConn)
> Dim dt As DataTable
> Dim sqlParams() As SqlParameter = { _
> New SqlParameter("@rid", rid)}
>
> dt = db.FillDataTable("usp_files_s_one", sqlParams)
>
> Dim fileData() As Byte = CType(dt.Rows(0)("FileData"), Byte())
>
> Response.Clear()
> Response.ClearHeaders()
> Response.ClearContent()
> Response.ContentType = "Application/msword"
> Response.OutputStream.Write(fileData, 0, fileData.Length)
> Response.OutputStream.Flush()
> Response.OutputStream.Close()
> ----------------------
>
>
>> "Jason Chu" <JasonChu@discussions.microsoft.com> wrote in message
>> news:BA02D945-1DF4-4EA0-B940-0BD2E9F4CB17@microsoft.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
>>>>
>>>>
>>>>

>>
>>

>
>




Simon 11-08-2005 10:45 PM

Re: Opening MS WORD document from database
 
Hmm, so I was somewhat unsure how to "delete everything except for the
directive." I believe the page directive is the topmost line that starts
with <%@ Page Language="vb"..... etc, etc. Is that right? I made a new,
empty webform that has nothing but the page directive in the .aspx. I tried
opening the file with this page using the following code in the code-behind.
The downloaded file doesn't contain the source of the webpage anymore, but
instead, a line of squares. Any file i try to download gives me the same
result =( Does anyone have sample code for downloading MS Word documents
from the database? Thanks.

---[begin vb.net code]----

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim db As New MD_Runs.Database(strDBConn)
Dim dt As DataTable
file_id = CInt(Request.QueryString("file_id"))
Dim sqlParams() As SqlParameter = {New SqlParameter("@file_id",
file_id)}

dt = db.FillDataTable("usp_files_s_one", sqlParams)

Dim fileData() As Byte = CType(dt.Rows(0)("fileData"), Byte())

Response.Clear()
Response.ClearHeaders()
Response.ClearContent()

Response.ContentType = "Application/msword"
Response.OutputStream.Write(fileData, 0, fileData.Length)
Response.OutputStream.Flush()
Response.OutputStream.Close()

End Sub
End Class

---[end code]---


"Daniel Walzenbach" <daniel.walzenbach@newsgroup.nospam> wrote in message
news:eL5pGGb4FHA.3880@TK2MSFTNGP12.phx.gbl...
> Simon,
>
> make sure to delete everything except the page directive from your .aspx
> page. That should do the trick!
>
> Regards
>
> Daniel
>
>
> "Simon" <sbee@leupold.com> schrieb im Newsbeitrag
> news:%23YxeiIY4FHA.700@TK2MSFTNGP15.phx.gbl...
>>I tried putting it into a response.outputstream, but still not getting the
>>actual file. Now, it displays the source of the web page instead. What
>>am I doing wrong?
>>
>> ----[VB.net code]-----
>> Dim db As New MD_Runs.Database(strDBConn)
>> Dim dt As DataTable
>> Dim sqlParams() As SqlParameter = { _
>> New SqlParameter("@rid", rid)}
>>
>> dt = db.FillDataTable("usp_files_s_one", sqlParams)
>>
>> Dim fileData() As Byte = CType(dt.Rows(0)("FileData"), Byte())
>>
>> Response.Clear()
>> Response.ClearHeaders()
>> Response.ClearContent()
>> Response.ContentType = "Application/msword"
>> Response.OutputStream.Write(fileData, 0, fileData.Length)
>> Response.OutputStream.Flush()
>> Response.OutputStream.Close()
>> ----------------------
>>
>>
>>> "Jason Chu" <JasonChu@discussions.microsoft.com> wrote in message
>>> news:BA02D945-1DF4-4EA0-B940-0BD2E9F4CB17@microsoft.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
>>>>>
>>>>>
>>>>>
>>>
>>>

>>
>>

>
>




Daniel Walzenbach 11-09-2005 09:17 PM

Re: Opening MS WORD document from database
 
Simon,

try the following:

Response.ContentType = "Application/msword"
Response.BinaryWrite(fileData)
Response.AddHeader("Content-Disposition", "inline; filename=test.doc")

Does this help?

Regards

Daniel Walzenbach


"Simon" <sbee@leupold.com> schrieb im Newsbeitrag
news:uCQ0cYL5FHA.3592@TK2MSFTNGP12.phx.gbl...
> Hmm, so I was somewhat unsure how to "delete everything except for the
> directive." I believe the page directive is the topmost line that starts
> with <%@ Page Language="vb"..... etc, etc. Is that right? I made a new,
> empty webform that has nothing but the page directive in the .aspx. I
> tried opening the file with this page using the following code in the
> code-behind. The downloaded file doesn't contain the source of the webpage
> anymore, but instead, a line of squares. Any file i try to download gives
> me the same result =( Does anyone have sample code for downloading MS
> Word documents from the database? Thanks.
>
> ---[begin vb.net code]----
>
> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
>
> Dim db As New MD_Runs.Database(strDBConn)
> Dim dt As DataTable
> file_id = CInt(Request.QueryString("file_id"))
> Dim sqlParams() As SqlParameter = {New SqlParameter("@file_id",
> file_id)}
>
> dt = db.FillDataTable("usp_files_s_one", sqlParams)
>
> Dim fileData() As Byte = CType(dt.Rows(0)("fileData"), Byte())
>
> Response.Clear()
> Response.ClearHeaders()
> Response.ClearContent()
>
> Response.ContentType = "Application/msword"
> Response.OutputStream.Write(fileData, 0, fileData.Length)
> Response.OutputStream.Flush()
> Response.OutputStream.Close()
>
> End Sub
> End Class
>
> ---[end code]---
>
>
> "Daniel Walzenbach" <daniel.walzenbach@newsgroup.nospam> wrote in message
> news:eL5pGGb4FHA.3880@TK2MSFTNGP12.phx.gbl...
>> Simon,
>>
>> make sure to delete everything except the page directive from your .aspx
>> page. That should do the trick!
>>
>> Regards
>>
>> Daniel
>>
>>
>> "Simon" <sbee@leupold.com> schrieb im Newsbeitrag
>> news:%23YxeiIY4FHA.700@TK2MSFTNGP15.phx.gbl...
>>>I tried putting it into a response.outputstream, but still not getting
>>>the actual file. Now, it displays the source of the web page instead.
>>>What am I doing wrong?
>>>
>>> ----[VB.net code]-----
>>> Dim db As New MD_Runs.Database(strDBConn)
>>> Dim dt As DataTable
>>> Dim sqlParams() As SqlParameter = { _
>>> New SqlParameter("@rid", rid)}
>>>
>>> dt = db.FillDataTable("usp_files_s_one", sqlParams)
>>>
>>> Dim fileData() As Byte = CType(dt.Rows(0)("FileData"), Byte())
>>>
>>> Response.Clear()
>>> Response.ClearHeaders()
>>> Response.ClearContent()
>>> Response.ContentType = "Application/msword"
>>> Response.OutputStream.Write(fileData, 0, fileData.Length)
>>> Response.OutputStream.Flush()
>>> Response.OutputStream.Close()
>>> ----------------------
>>>
>>>
>>>> "Jason Chu" <JasonChu@discussions.microsoft.com> wrote in message
>>>> news:BA02D945-1DF4-4EA0-B940-0BD2E9F4CB17@microsoft.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
>>>>>>
>>>>>>
>>>>>>
>>>>
>>>>
>>>
>>>

>>
>>

>
>





All times are GMT. The time now is 12:22 AM.

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