<% Response.Buffer=True %>
<!--#include file="conn/conn_index.asp"-->
<!--#include file="inc/setup.asp"-->
<!--#include file="inc/char.asp"-->
<!--#include file="inc/sql.asp"-->
<%
	end if
	
	Response.Clear
	
	dim url
	Dim fso,fl,flsize
	dim Dname
	
	Dim objStream,ContentType,flName,isre,url1
	
	user_agent=Request.ServerVariables("HTTP_USER_AGENT")
	http_reffer=Request.ServerVariables("HTTP_REFERER")
	server_name=Request.ServerVariables("SERVER_NAME")
	'检查当前用户是否是蜘蛛人
	
	function check(user_agent)
	
		allow_agent=split("Baiduspider,Scooter,ia_archiver,Googlebot,FAST-WebCrawler,MSNBOT,Slurp",",")
		check_agent=false
		for agenti=lbound(allow_agent) to ubound(allow_agent)
		
		if instr(user_agent,allow_agent(agenti))>0 then
			check_agent=true
			exit for
		end if
		
		next
		check=check_agent
		
	end function
	
	Function Cur_Url()    '获取当前页面URL的函数
	  Domain_Name = LCase(Request.ServerVariables("Server_Name"))
	  Page_Name = LCase(Request.ServerVariables("Script_Name"))
	  Quary_Name = LCase(Request.ServerVariables("Quary_String"))
	  If Quary_Name ="" Then
		Cur_Url = "http://"&Domain_Name&Page_Name
	  Else
		Cur_Url = "http://"&Domain_Name&Page_Name&"?"&Quary_Name
	  End If
	End Function
	id1=Trim(Request.QueryString("id"))
	if check(user_agent)=False then
		if http_reffer="" or left(http_reffer,len("http://"&server_name)+1)<>"http://"&server_name&"/" then
			call Cur_Url()
			Response.Redirect("Error.asp?type=cj&url="&Cur_Url&"?id="&id1&"")
			response.end
		end if
	end if
	
	'*********************************************调用时传入的下载文件名
	id=int(ReplaceBadChar(Trim(Request.QueryString("id"))))
	'******************************************************************
	If id<>"" and id<>0 and id&"a"<>"a" Then
		'******************************下载文件存放的服务端目录
		set rs_down=server.createobject("adodb.recordset")
		sql="select down.url,dtype.folder from down,dtype where down.id="&id&" and down.dtype=dtype.id" 
		rs_down.open sql,conn,1,1
		if rs_down.eof then
			response.write"<SCRIPT language=JavaScript>alert('您的提交有误，现在为您返回');"
			response.write"location.href='down.asp';</SCRIPT>"
			response.end
		else
			url=rs_down("url")
			folder=rs_down("folder")
		end if
		rs_down.close
		set rs_down=nothing
		url=server.MapPath("down/")&"\"&folder&"\"&url&""
		'***************************************************
	else
		response.write"<SCRIPT language=JavaScript>alert('您的提交有误，现在为您返回');"
		response.write"location.href='down.asp';</SCRIPT>"
		response.end
	End If
	
	Set fso=Server.CreateObject("Scripting.FileSystemObject")
	Set fl=fso.getfile(url)
	flsize=fl.size
	flName=fl.name
	Set fl=Nothing
	Set fso=Nothing
	Set objStream = Server.CreateObject("ADODB.Stream")
	objStream.Open
	objStream.Type = 1
	objStream.LoadFromFile url
	
	
	Select Case lcase(Right(flName, 4))
	Case ".asf"
	ContentType = "video/x-ms-asf"
	Case ".avi"
	ContentType = "video/avi"
	Case ".doc"
	ContentType = "application/msword"
	Case ".zip"
	ContentType = "application/zip"
	Case ".xls"
	ContentType = "application/vnd.ms-excel"
	Case ".gif"
	ContentType = "image/gif"
	Case ".jpg", "jpeg"
	ContentType = "image/jpeg"
	Case ".wav"
	ContentType = "audio/wav"
	Case ".mp3"
	ContentType = "audio/mpeg3"
	Case ".mpg", "mpeg"
	ContentType = "video/mpeg"
	Case ".rtf"
	ContentType = "application/rtf"
	Case ".htm", "html"
	ContentType = "text/html"
	Case ".txt"
	ContentType = "text/plain"
	Case Else
	ContentType = "application/octet-stream"
	End Select
	
	Response.AddHeader "Content-Disposition", "attachment; filename=" & flName
	Response.AddHeader "Content-Length", flsize
	
	Response.Charset = "UTF-8"
	Response.ContentType = ContentType
	
	Response.BinaryWrite objStream.Read
	Response.Flush
	response.Clear()
	objStream.Close
	Set objStream = Nothing
	
	call closeconn()
%>