<%
'**************************************************************
'**使用方法: **
'**在點擊下載處加連接<a href="load.asp?filename=文件名"></a> **
'**陽光白雪——2005年11月21日 **
'**E-mail:chenmanyi0818@126.com **
'**HomePage:http://www.toumh.com ; **
'**************************************************************
'**************************************************
'** 實現文件下載函數 **
'**************************************************
Function Filedownload(filename)
Dim strchar,fliesend,objectFile,objfile,objStream,path,pathtype
pathtype = "application/x-msdownload"
path = server.MapPath(filename)
Const loadfilesize=32768 '32KB,也可取其它值,單位:字節
'在給path賦值后,其最終值必定是該圖片在服務器端存儲器上的絕對路徑,如 C:\Inetpub\wwwroot\pic\help.gif
'因為 server.MapPath 取得的只是站點根目錄的路徑,所以在后面還應加上文件所在的文件夾再加文件名
'例如:要下載的文件 setup.exe 在根目錄的 download 文件夾下,則 path = server.MapPath("download/"&filename)
'***********************************
'測試用
'response.Write(path)
'response.Write(filename)
'response.End()
'***********************************
fliesend=0
TransferFile = True
Set objectFile = Server.CreateObject("Scripting.FileSystemObject")
Set objfile = objectFile.GetFile(Path)
Set objStream = objfile.OpenAsTextStream(1,-1)
Response.AddHeader "content-type", pathtype
response.AddHeader "Content-Disposition","attachment;filename="&filename
Response.AddHeader "content-length", objfile.Size
Do While Not objStream.AtEndOfStream
strchar = objStream.Read(1)
Response.BinaryWrite(strchar)
fliesend = fliesend + 1
If (fliesend MOD loadfilesize) = 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
end if
objStream.Close
Set objStream = Nothing
Set objectFile = Nothing
End Function
'****************************************************
'** 文件下載函數結束 **
'****************************************************
Dim fileneme,downloadfile
filename = request("filename") '此處的 filename 僅為下載文件的名稱(包括擴展名)
downloadfile = Filedownload(filename) '調用文件下載函數
Response.End
%>