| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 590 人关注过本帖, 2 人收藏
标题:一个通过ASP下载文件的代码[ 支持断点,多线程 ]
取消只看楼主 加入收藏
kira007
Rank: 2
等 级:论坛游民
帖 子:294
专家分:27
注 册:2007-6-28
结帖率:50%
收藏(2)
 问题点数:0 回复次数:0 
一个通过ASP下载文件的代码[ 支持断点,多线程 ]
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%Function ShowError(str)%>
<script language="javascript">
    alert("<%=str%>");
    location.target="_blank";
    location.href="../vip/login.asp " ;
   </script>
<%End Function
on error resume next
FileName=Trim(Request("FileName"))
Path=Trim(Request("Path"))

'防止盗链
From_url = Cstr(Request.ServerVariables("HTTP_REFERER"))  
Serv_url = Cstr(Request.ServerVariables("SERVER_NAME"))  
if mid(From_url,8,len(Serv_url)) <> Serv_url then  
   response.write "非法链接!"
   response.end  
end if  

'检查权限   
   Path="E:\hello"

Const adTypeBinary=1

if FileName="" Then
   Response.Write"无效文件名."
   Response.End
End if
'下面是不希望下载的文件
FileExt=Mid(FileName,InStrRev(FileName,".")+1)

Select Case UCase(FileExt)
   Case "ASP","ASA","ASPX","ASAX","MDB"
    Response.Write"受保护文件,不能下载."
    Response.End
End Select

set fileobj=server.CreateObject("Scripting.FileSystemObject")
dim retval
dim found
found=false

call FindFile(Path,FileName,retval)
if not fileobj.FileExists(retval) then
       response.Write "没有找到文件,请与管理员联系"
       response.End()
end if
set f=fileobj.GetFile(retval)
fileLength=f.Size
set fileobj=nothing
position=0
Response.Clear

'支持断点,多线程 下载文件
range=Request.ServerVariables("HTTP_Range")
if trim(range)<>"" then
      Response.Status="206"
      position=Clng(Replace(Replace(range,"bytes=",""),"-",""))
end if

if position<>0 then
      Response.AddHeader "Content-Range","bytes "+CStr(position)+"-"+CStr(fileLength-1)+"/"+CStr(fileLength)
end if

Response.ContentType="application/octet-stream"
Response.AddHeader "content-disposition","attachment;filename="&FileName
Response.AddHeader "Content-Length",filelength-position

Set Stream=server.CreateObject("ADODB.Stream")
Stream.Type=adTypeBinary
Stream.Open

Stream.LoadFromFile retval
Stream.Position=position
While Not Stream.EOS
   Response.BinaryWrite Stream.Read(1024*64)
Wend
Stream.Close
Set Stream=Nothing
Response.Flush
Response.End


function FindFile(path,filename,ByRef reval)
        if found then
              exit function
        end if
        set folder=fileobj.GetFolder(path)
        if fileobj.FileExists(folder.path&"\"&filename) then
              reval=folder.path&"\"&filename
              found=true
              Exit Function
       else
              set folders=fileobj.GetFolder(path).SubFolders
              if folders.count<>0 then
                     for each myitem in folders
                           if fileobj.FileExists(myitem.path&"\"&filename) then
                                  reval=myitem.path& "\" & filename
                                  found=true
                                  exit for
                            else
                                   Call FindFile(myitem.path,filename,reval)
                           end if
                     next
               end if
               set folders=nothing
         end if
End Function
%>

 
搜索更多相关主题的帖子: asp 
2008-10-11 17:41
快速回复:一个通过ASP下载文件的代码[ 支持断点,多线程 ]
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017345 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved