Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening word files from ASP

Status
Not open for further replies.

amutinu

Programmer
Oct 19, 2001
50
US
Hello All,
I have a question regarding opening external files from ASP. I have a database where the actual binary object (word document) is stored. I want to be able to retrieve that document and then open it from ASP Page.

Does anyone know how to do this?

Your help would be greatly appreciated.

Thanks,
 
While you would normally set a link to a word document - in this case you cannot. The key is in setting the correct header:

<%@ Language=VBScript %>
<SCRIPT id=DebugDirectives runat=server language=javascript>
// Set these to true to enable debugging or tracing
@set @debug=false
@set @trace=false
</SCRIPT>
<%Option Explicit%>
<%
'double-check that a valid login has occured...
If Session(&quot;LoggedIn&quot;) <> True then
Response.Redirect &quot;../default.asp&quot;
end if
%>
<SCRIPT LANGUAGE=vbscript RUNAT=Server>

' -- Tell the browser the data is a
' -- Comma Separated Values List (CSV)
Response.ContentType=&quot;application/doc&quot;
' -- Tell the browser to associate a file name
' -- with the data
Response.AddHeader &quot;Content-Disposition&quot;, &quot;filename=worddoc.doc;&quot;


I am not sure what the value for the ContentType should be for word. For a csv (or excel file) the value is &quot;application/csv&quot;.

After this header, just read in the word binary from the database and pump it out to the page. No HTML tags are needed - like <HTML> or <HEAD> or <BODY> - just the binary.

Tell me if this works - I'll be doing the same for PDF files soon (using XSLT, not ASP)! (Content Management)
 
Well thanks for your response. I used the following code to retrieve the binary data from the database but then when i try to display it, it gives me the following errror:

<font face=&quot;Arial&quot; size=2>
<p>Response object</font> <font face=&quot;Arial&quot; size=2>error 'ASP 0106 : 80020005'</font>
<p>
<font face=&quot;Arial&quot; size=2>Type Mismatch</font>
<p>
<font face=&quot;Arial&quot; size=2>/Authenticated/Profile/test.asp</font><font face=&quot;Arial&quot; size=2>, line 34</font>
<p>
<font face=&quot;Arial&quot; size=2>An unhandled data type was encountered.
</font>

Not sure what this means.

I am able to retrieve the binary data from the database but cannot display it. I have an excel file but would be the same for word document.
And ideas??


My Code is as follows:

Dim rs
Dim ConstConnectString
Dim strSQL
Dim intAttachmentID

intAttachmentID = Request.QueryString(&quot;AttachmentID&quot;)

ConstConnectString = &quot;Provider=SQLOLEDB;User ID=EK_Admin;Initial Catalog=EK;Data Source=(local);Connect Timeout=60&quot;

Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)

strSQL = &quot;SELECT [Attachment] FROM Attachment&quot; & _
&quot; WHERE AttachmentID = &quot; & intAttachmentID & &quot;&quot;

rs.Open strSQL, ConstConnectString, 2, 4

If Not rs.EOF then
Response.ContentType = &quot;application/x-msexcel&quot;
Response.BinaryWrite rs(&quot;Attachment&quot;)
End If

rs.Close
Set rs = Nothing
 
So what code is on line 34 of test.app?

The code seems OK, See the microsoft support article Q173308 (which is in the VI help library) - this adds a number of lines to remove other header entries...
' Clear out the existing HTTP header information
Response.Expires = 0
Response.Buffer = TRUE
Response.Clear

I do not know if you need use 'getChunck' methods to extract the data from the BLOB database column. (Content Management)
 
The code on line 34 is:
Response.BinaryWrite rs(&quot;Attachment&quot;)

Somehow i still get the &quot;Type Mismatch&quot; error after inserting the following code.

Response.Expires = 0
Response.Buffer = TRUE
Response.Clear

I also looked at the msdn for Q173308 but that also didn't help. Somehow response.binarywrite is not matching my content type.

Any thoughts?

thanks,
 
The rs(&quot;Attachment&quot;) could return two things - the column value OR the column itself (as an object pointer).

I am guessing, but you may want to be a bit more explicit:

rs(&quot;Attachment&quot;).value

or

rs(&quot;Attachment&quot;).getChunk(1000) [or something like that]

I really do not know more than this - perhaps someone else has a working model? (Content Management)
 
I got this to work. somehow it was the mime type that wasn't setting it right

response.contenttype = &quot;application/vnd-ms-excel&quot; did the trick.
 
What did your code end up looking like?
I'm trying to do the exact same thing, but with a Word document. All I seem to get to display is either 1) binary data (boxes and such) or 2) a bunch of document information stored by Word.
What type of database are you using? I'm currently using Access using an &quot;OLE object&quot; data type with a link to a Word document, but I'll need to convert this to a SQL Server database in a few months...it converts as an &quot;Image&quot; datatype.
 
Hello LindaH,

The code is attached below. All i had to change was the MIME type on my ASP Page. I am using Sql Server as my back end database. In your case (for word documents) you will have to change the MIME type to &quot;application/vnd-msword&quot; i think. But you may want to research on MSDN for the exact MIME type.

Hope this helps!!

Dim rs
Dim ConstConnectString
Dim strSQL
Dim intAttachmentID

intAttachmentID = Request.QueryString(&quot;AttachmentID&quot;)

ConstConnectString = &quot;Provider=SQLOLEDB;User ID=EK_Admin;Initial Catalog=EK;Data Source=(local);Connect Timeout=60&quot;

Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)

strSQL = &quot;SELECT [Attachment] FROM Attachment&quot; & _
&quot; WHERE AttachmentID = &quot; & intAttachmentID & &quot;&quot;

rs.Open strSQL, ConstConnectString, 2, 4

If Not rs.EOF then
Response.ContentType = &quot;application/vnd-ms-excel&quot;
Response.BinaryWrite rs(&quot;Attachment&quot;)
End If

rs.Close
Set rs = Nothing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top