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="include\connect.asp"-->
<%
' 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 "Download" 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("ADODB.Stream"
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile Server.MapPath(strFilePath & Request("FileName"
)
If Request("Download"
= 1 Then
' Promt User to download the document.
Response.AddHeader "Content-Disposition", "attachment; filename=" & Request("FileName"
Response.AddHeader "Content-Length", strFileSize
Response.Charset = "UTF-8"
Else
' Display the document in the browser window.
Response.AddHeader "Content-Disposition", "filename=" & Request("FileName"
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, ".doc", "application/msword", "doc/"
Set rsContentType = db.Execute("SELECT * FROM ContentType WHERE FileType='"&lcase(Right(Request("FileName"
, 4))&"'"
If rsContentType.EOF Then
Response.Write "Content Type Not Supported"
Set rsContentType=Nothing
Else
strContentType = rsContentType("strContentType"
strFilePath = rsContentType("strFilePath"
Set rsContentType=Nothing
Call viewStaticFile()
End If
%>
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="include\connect.asp"-->
<%
' 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 "Download" 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("ADODB.Stream"
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile Server.MapPath(strFilePath & Request("FileName"
If Request("Download"
' Promt User to download the document.
Response.AddHeader "Content-Disposition", "attachment; filename=" & Request("FileName"
Response.AddHeader "Content-Length", strFileSize
Response.Charset = "UTF-8"
Else
' Display the document in the browser window.
Response.AddHeader "Content-Disposition", "filename=" & Request("FileName"
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, ".doc", "application/msword", "doc/"
Set rsContentType = db.Execute("SELECT * FROM ContentType WHERE FileType='"&lcase(Right(Request("FileName"
If rsContentType.EOF Then
Response.Write "Content Type Not Supported"
Set rsContentType=Nothing
Else
strContentType = rsContentType("strContentType"
strFilePath = rsContentType("strFilePath"
Set rsContentType=Nothing
Call viewStaticFile()
End If
%>