作家
登录

利用xmlhttp和adodb.stream加缓存技术下载远程Web文件

作者: 来源:www.28hudong.com 2013-03-30 09:06:59 阅读 我要评论

<%'----------远程获取内容,并将内容存在本地电脑上,包括任何文件!----------'---------------利用xmlhttp和adodb.stream-----------------'On Error Resume Next'-------------------------------定义输出格式-----------------------------path=request("path")if path ="" thenpath="http://pcqc.86516.com/index.asp"'这里定义的网址是百度,,注意一定要有文件后缀end if sPath = Pathif left(lcase(path),7) <> "http://" then'-------------如果前面没有http就是本地文件,交给LocalFile处理------------LocalFile(path)else'--------------------否则为远程文件,交给RemoteFile处理------------------RemoteFile(Path)end if'Response.Write err.Description'--------------处理函数----------- sub LocalFile(Path)'-------------------如果为本地文件则简单的跳转到该页面-------------------'Response.Redirect PathResponse.write "发生错误!"End SubSub RemoteFile(sPath)'-------------------------处理远程文件函数------------------------------FileName = GetFileName(sPath)'-------------GetFileName为把地址转换为合格的文件名过程-------------FileName = Server.MapPath("Cache/" & FileName)Set objFso = Server.CreateObject("Scripting.FileSystemObject")'Response.Write fileNameif objFso.FileExists(FileName) Then'--------------检查文件是否是已经访问过,如是,则简单跳转------------Response.Redirect "cache/" & GetFileName(path)Else'----------------否则的话就先用GetBody函数读取----------------------'Response.Write Patht = GetBody(Path)'-----------------用二进制方法写到浏览器上--------------------------Response.BinaryWrite tResponse.Flush'-----------------输出缓冲------------------------------------------SaveFile t,GetFileName(path)'------------------将文件内容缓存到本地路径,以待下次访问-----------End ifSet objFso = NothingEnd Sub Function GetBody(url)'-----------------------本函数为远程获取内容的函数---------------------'on error resume next'Response.Write urlSet Retrieval = CreateObject("Microsoft.XMLHTTP")'----------------------建立XMLHTTP对象-----------------------------With Retrieval.Open "Get", url, False, "", ""'------------------用Get,异步的方法发送-----------------------.Send'GetBody = .ResponseTextGetBody = .ResponseBody'------------------函数返回获取的内容--------------------------End WithSet Retrieval = Nothing'response.Write err.DescriptionEnd Function Function GetFileName(str)'-------------------------本函数为合格化的文件名函数-------------------str = Replace(lcase(str),"http://","")str = Replace(lcase(str),"//","/")str = Replace(str,"?","")str = Replace(str,"&","")str = Replace(str,"/","")str = replace(str,vbcrlf,"")GetFileName = strEnd Function sub SaveFile(str,fName)'-------------------------本函数为将流内容存盘的函数-------------------'on error resume nextSet objStream = Server.CreateObject("ADODB.Stream")'--------------建立ADODB.Stream对象,必须要ADO 2.5以上版本---------'objStream.Type = adTypeBinaryobjStream.Type = 1'-------------以二进制模式打开-------------------------------------objStream.Openobjstream.write str'--------------------将字符串内容写入缓冲--------------------------'response.Write fname'路径注意objstream.SaveToFile "E:webrootpcqcvipUploadFilecache"&fName,2'objstream.SaveToFile "d:cache" & fName,adSaveCreateOverWrite'--------------------将缓冲的内容写入文件--------------------------'response.BinaryWrite objstream.Readobjstream.Close()set objstream = nothing'-----------------------关闭对象,释放资源-------------------------'response.Write err.DescriptionEnd sub function saveimage(from,tofile)dim geturl,objStream,imgsgeturl=trim(from)imgs=gethttppage(geturl)'取得图片的具休内容的过程Set objStream = Server.CreateObject("ADODB.Stream")'建立ADODB.Stream对象,必须要ADO 2.5以上版本objStream.Type =1'以二进制模式打开objStream.Openobjstream.write imgs'将字符串内容写入缓冲objstream.SaveToFile server.mappath(tofile),2'-将缓冲的内容写入文件objstream.Close()'关闭对象set objstream=nothingend function%>

  推荐阅读

  以前写的一个分页存储过程,刚才不小心翻出来的

CREATE PROCEDURE GoalerPageSp@IntPageSize int,@IntCurrPage int,@strFields nvarchar(2000),@strTable varchar(200),@strWhere varchar(800),@strOrderType varchar(200),@strKeyField varchar(50)ASSET NOCOUN>>>详细阅读


本文标题:利用xmlhttp和adodb.stream加缓存技术下载远程Web文件

地址:http://www.17bianji.com/kaifa2/ASP/33301.html

关键词: 探索发现

乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

网友点评
自媒体专栏

评论

热度

精彩导读
栏目ID=71的表不存在(操作类型=0)