<a href="?action=file&filename=aaaa.doc">生成aaaa.doc文档</a>
该方法有一个不好的地方,就是所有的生成文件全是先保存在服务器上,然后再下载到本地。
<%
actions = trim(request("action"))
filenames=trim(request("filename")) '得到生成的文件名,待会用于生成与下载
if actions = "file" then
dim gongame '其实这个本来是想用模板,后来因为数据真的太多,也懒得弄了。下面的只是一个示例
gongame = "<table cellpadding='0' cellspacing='1' class='utable' height='16'><tr><td class='utabletd8' height='16'><strong>系统</strong> - 个人简历</td></tr></table><table cellpadding='0' cellspacing='1' class='utable'><tr><td class='utabletd7'>信息来源:自由注册 注册日期:"& time() &"</td></tr></table>"
Dim strTemplate
Dim FileObject '声明一个FileSystemObject对象实例
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
Dim TextFile '声明一个TextStream对象实例
Set TextFile= FileObject.CreateTextFile(Server.MapPath("word/"&filenames)) '把生成的文件存放在 word 目录下。
TextFile.Write gongame
Set TextFile=Nothing
'以下代码是用来下载刚才生成aaaa.doc 文件
Const ForReading=1
Const TristateTrue=-1
Const FILE_TRANSFER_SIZE=16384
Response.Buffer = True
Function TransferFile(path, mimeType, filename)
Dim objFileSystem, objFile, objStream
Dim char
Dim sent
send=0
TransferFile = True
Set objFileSystem = Server.CreateObject("Scripting.FileSystemObject")
Set objFile = objFileSystem.GetFile(Path)
Set objStream = objFile.OpenAsTextStream(ForReading, TristateTrue)
Response.AddHeader "content-type", mimeType
response.AddHeader "Content-Disposition","attachment;filename=" & filename
Response.AddHeader "content-length", objFile.Size
Do While Not objStream.AtEndOfStream
char = objStream.Read(1)
Response.BinaryWrite(char)
sent = sent + 1
If (sent MOD FILE_TRANSFER_SIZE) = 0 Then
Response.Flush
If Not Response.IsClientConnected Then
TransferFile = False
Exit Do
End If
End If
Loop
Response.Flush
If Not Response.IsClientConnected Then TransferFile = False
objStream.Close
Set objStream = Nothing
Set objFileSystem = Nothing
End Function
Dim path, mimeType, sucess,downfilename
downfilename="word/"&request("filename")
path = Server.MapPath(downfilename)
mimeType="text/plain"
sucess = TransferFile(path, mimeType,downfilename)
Response.End
end if
%>