473,659 Members | 2,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.EventArg s) Handles Me.Load

Dim con As New
System.Data.Sql Client.SqlConne ction("server=m yServer;databas e=myDatabase;in tegrated
security=true")
Dim cmd As New System.Data.Sql Client.SqlComma nd("Select * From
Documents where id=" & Request.QuerySt ring("documentI d"), con)

con.Open()
Dim dr As System.Data.Sql Client.SqlDataR eader = cmd.ExecuteRead er

If dr.Read Then
Response.Clear( )
Response.AddHea der("Content-Type", "applicatio n/msword")
Response.AddHea der("Content-disposition", "attachment ;
filename=myword .docx")
Response.Binary Write(CType(dr( "data"), Byte()))
End If

con.Close()
con = Nothing

End Sub

</script>

***************
Mar 26 '07 #1
8 3664

"Jeppe Dige Jespersen" <jd*@jdj.dkwrot e in message
news:uo******** ******@TK2MSFTN GP03.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.EventArg s) Handles Me.Load

Dim con As New
System.Data.Sql Client.SqlConne ction("server=m yServer;databas e=myDatabase;in tegrated
security=true")
Dim cmd As New System.Data.Sql Client.SqlComma nd("Select * From
Documents where id=" & Request.QuerySt ring("documentI d"), con)

con.Open()
Dim dr As System.Data.Sql Client.SqlDataR eader = cmd.ExecuteRead er

If dr.Read Then
Response.Clear( )
Response.AddHea der("Content-Type", "applicatio n/msword")
Response.AddHea der("Content-disposition", "attachment ;
filename=myword .docx")
Response.Binary Write(CType(dr( "data"), Byte()))
End If

con.Close()
con = Nothing

End Sub

</script>

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

Maybe this helps

Response.Buffer = True
Mar 26 '07 #2
>
Maybe this helps

Response.Buffer = True
Doesn't solve the problem, but thanks for trying :-)

/j.jespersen
Mar 26 '07 #3
On Mar 26, 1:24 pm, "Jeppe Dige Jespersen" <j...@jdj.dkwro te:
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.
Mar 26 '07 #4
>
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


Mar 26 '07 #5
On Mar 26, 1:46 pm, "Jeppe Dige Jespersen" <j...@jdj.dkwro te:
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.AddHea der("Content-Type", "applicatio n/msword")

-------------------------

Response.Clear( )
Response.AddHea der("Content-disposition", "attachment ;
filename=myword .docx")
Response.Binary Write(CType(dr( "data"), Byte()))

Mar 26 '07 #6
>
Response.AddHea der("Content-Type", "applicatio n/msword")
Same result. :-(

/j.jespersen
Mar 26 '07 #7
On Mar 26, 4:11 pm, "Jeppe Dige Jespersen" <j...@jdj.dkwro te:
Response.AddHea der("Content-Type", "applicatio n/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.Conten tEncoding = System.Text.Enc oding.UTF8
Response.Conten tType = "applicatio n/vnd.ms-word.document.1 2"

As you have no problem to open the file from WinForms. I think your
problem is in encoding, although maybe "vnd.ms-word.document.1 2" is
also important...

Please try

Mar 26 '07 #8
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" <jd*@jdj.dkwrot e in message
news:uo******** ******@TK2MSFTN GP03.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.EventArg s) Handles Me.Load

Dim con As New
System.Data.Sql Client.SqlConne ction("server=m yServer;databas e=myDatabase;in tegrated
security=true")
Dim cmd As New System.Data.Sql Client.SqlComma nd("Select * From
Documents where id=" & Request.QuerySt ring("documentI d"), con)

con.Open()
Dim dr As System.Data.Sql Client.SqlDataR eader = cmd.ExecuteRead er

If dr.Read Then
Response.Clear( )
Response.AddHea der("Content-Type", "applicatio n/msword")
Response.AddHea der("Content-disposition", "attachment ;
filename=myword .docx")
Response.Binary Write(CType(dr( "data"), Byte()))
End If

con.Close()
con = Nothing

End Sub

</script>

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

Mar 27 '07 #9

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

Similar topics

1
1960
by: fibreiv | last post by:
I am trying to download files from my database that I uploaded to it. I can download bmp, txt file but have not been able to d/l pdf files. I am able to d/l pdf files stored in the file system but not in the database. Any help would be great. Here is my code, I have hard coded the file to download for testing purposes. Connect = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Password=;Initial Catalog=INNB;Data...
6
307
by: Shawn | last post by:
Hi. How can I download a file and store it on the web server. I have a complete URL to the file, but I never know what kind of file it is. It can be pdf, jpg, tif, doc, xls etc. Thanks, Shawn
2
2430
by: Bala | last post by:
Hi I am trying to download the PDF files from my webserver using ASP.Net. All my files are stored at F Drive on webserver. Like this F:\Main Folder\Sub Folder\Files\File1.pdf I am impersonating the domain user account and downloading the file, It seems the impersonate is got success, but its not download the file. Its simply downloading some junk pdf file instead of original file.
4
2928
by: Joe | last post by:
I'm hosting my web service on a Windows 2003 box which is remotely located. When trying to add a web reference to a C# project I get an error message 'There was an error downloading 'http://mydomain.com:port/webservice.asmx' The operation has timed-out (I've tried with and without using a separate port for the service) The weird thing is the page does show up on the left side of the screen listing the available methods but the Add...
1
2069
by: just.starting | last post by:
Hi, My dot net client downloads files and checks for any new files time to time. The server is apache2.0.53 server. So what happens is that my file download thing works fine if I dont try to call any page from the server while downloading. If I try to call a single page while downloading a file then the page request goes time out and the server then closes the existing download stream and the client doestn't throw any exception. So many a...
3
2786
by: just.starting | last post by:
Hi, My dot net client downloads files and checks for any new files time to time. The server is apache2.0.53 server. So what happens is that my file download thing works fine if I dont try to call any page from the server while downloading. If I try to call a single page while downloading a file then the page request goes time out and the server then closes the existing download stream and the client doestn't throw any exception. So many a...
14
2128
by: suryadithya | last post by:
Hi, I am very new to php. And I have got to submit a project very soon (By tomorrow evening). So please help. The problem is this. I have created a file upload mechanism where the uploaded files are stored under a different id for security something like 0001.pdf, 0002.jpg etc.. The original names are stored in a mysql database. Now I have a file download page. The links are the original names of the files like user1.pdf, user2.pdf... When...
4
3446
by: Nik0001 | last post by:
Hello everyone! I have the following problem I need to download several HTML pages and get meta-tags out of the code. I decided it would be better to download only the meta-tags rather than downloading the whole page. But the standard method (HttpWebRequest) in C# only allows me to download the whole page. Is there some alternative method?
12
2576
by: Queez | last post by:
I have a .NET assembly (ScanControl.dll), which accesses a scanner on my client's local machine and allows them to scan a page (Scan()). I have an ASP.NET web application with a page which will (theoretically) use this control to allow a user to scan a page into my database (Scan.aspx). I serve the control as an object on the page, like so: <object id="scanningObj" height="300px" width="500px"...
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8339
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8751
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4176
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2757
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.