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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print Static File to Browser with BinaryWrite & Stream

Status
Not open for further replies.

MagnusFox

IS-IT--Management
Jan 12, 2001
51
0
0
US
Good day all,

I read through many threads to compile this little script. Many thanks to all those who contributed pieces of this code in previous threads.

I have only one small issue with the script: file name when saving to disk.

The PDF documents work just fine. They prompt the user to save the document with the filename passed in the query string. However, other document types are prompting the user with the asp filename and its associated query string as the save as document name.

Is there a way to fill the save as prompt with the query string value for filename?

Many thanks,
Magnus

If you have any suggestions on how to make the code better, please post that also. Please help the next guy as much as possible!

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

' Required query string name value pairs:
' FileName=example.doc (must use 3 charecter file extentions)
' Optional query string name value pairs:
' Download=1 (promt user to save content to disk)

' Add your security check of the user here. If they are not logged in, don't run
' any more of this script.

<!--#include file=&quot;include\connect.asp&quot;-->
<%

' This function will print the contents of a static file to the browser window.
' It will use the Content Type associated with the document type to add the correct
' header to the browser. If the &quot;Download&quot; request variable is set to 1, it will
' promt the user to save the file.
function viewStaticFile()
Response.Buffer = True
Const adTypeBinary = 1
Response.Clear

Set objStream = Server.CreateObject(&quot;ADODB.Stream&quot;)
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile Server.MapPath(strFilePath & Request(&quot;FileName&quot;))

If Request(&quot;Download&quot;) = 1 Then
' Promt User to download the document.
Response.AddHeader &quot;Content-Disposition&quot;, &quot;attachment; filename=&quot; & Request(&quot;FileName&quot;)
Response.AddHeader &quot;Content-Length&quot;, strFileSize
Response.Charset = &quot;UTF-8&quot;
Else
' Display the document in the browser window.
Response.AddHeader &quot;Content-Disposition&quot;, &quot;filename=&quot; & Request(&quot;FileName&quot;)
Response.ContentType = strContentType
End If

Response.BinaryWrite objStream.Read
Response.Flush

objStream.Close
Set objStream = Nothing
end function

' Lookup the file type of the document sent in the query string. Use the last 4 charecters
' in the FileName value to get the file extention.
' Table ContentType has the following fields:
' ContentTypeID, FileType, strContentType, strFilePath (exapmle below)
' 1, &quot;.doc&quot;, &quot;application/msword&quot;, &quot;doc/&quot;
Set rsContentType = db.Execute(&quot;SELECT * FROM ContentType WHERE FileType='&quot;&lcase(Right(Request(&quot;FileName&quot;), 4))&&quot;'&quot;)
If rsContentType.EOF Then
Response.Write &quot;Content Type Not Supported&quot;
Set rsContentType=Nothing
Else
strContentType = rsContentType(&quot;strContentType&quot;)
strFilePath = rsContentType(&quot;strFilePath&quot;)
Set rsContentType=Nothing
Call viewStaticFile()
End If
%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top