用ASP实现文件下载
2016-07-14 19:53
633 查看
<%
'**************************************************************
'**使用方法: **
'**在点击下载处加连接<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
%>
'**************************************************************
'**使用方法: **
'**在点击下载处加连接<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
%>
相关文章推荐
- asp.net mvc ajax分页 CPager(秒杀杨涛ajax分页) 封装之前
- ASP.Net Core-TagHelpers
- ASP.NET Core Token认证
- asp.net mvc 之旅 —— 第六站 ActionFilter的应用及源码分析
- ASP.NET Web API 自定义MediaType实现jsonp跨域调用
- Asp.Net Unix时间戳和DateTime类型转换
- 转:ASP.NET 使用Ajax
- ASP.NET MD5加密
- 收藏:ASP.NET提供文件下载函数(支持大文件、续传、速度限制、资源占用小) (转自: Arhrun)
- 各版本IIS下ASP.net请求处理过程区别
- ASP.net:截取固定长度字符串显示在页面,多余部分显示为省略号
- ASP.NET发送电子邮件
- asp.net SessionState之sqlserver模式
- asp.net 如何引用dll
- [译]初识.NET Core & ASP.NET Core
- 【Asp.Net Core】一、Visual Studio 2015 和 .NET Core 安装
- 为何没有.aspx.designer.cs文件?
- 【转】asp.net(c#)加密解密算法之sha1、md5、des、aes实现源码详解
- ASP.NET Web API系列——选择Web API还是WCF
- 在 ASP.NET MVC 项目中使用 WebForm