欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP采集入库生成本地文件的几个函数

程序员文章站 2022-10-31 13:34:03
'*****************************************************************' function' 作用 :利用流保...

'*****************************************************************
' function
' 作用 :利用流保存文件
' 参数 :from(远程文件地址),tofile(保存文件位置)
'*****************************************************************
private function savefiles(byref from,byref tofile)
dim datas
datas=getdata(from,0)
response.write "保存成功:"&formatnumber(len(datas)/1024*2,2)&"kb"
response.flush
if formatnumber(len(datas)/1024*2,2)>1 then
ados.type = 1
ados.mode =3
ados.open
ados.write datas
ados.savetofile server.mappath(tofile),2
ados.close()
else
response.write "保存失败:文件大小"&formatnumber(len(imgs)/1024*2,2)&"kb,小于1k"
response.flush
end if
end function

'*****************************************************************
' function(私有)
' 作用 :利用fso检测文件是否存在,存在返回true,不存在返回false
' 参数 :filespes(文件位置)
'*****************************************************************
private function isexists(byref filespec)
if (fso.fileexists(server.mappath(filespec))) then
isexists = true
else
isexists = false
end if
end function

'*****************************************************************
' function(私有)
' 作用 :利用fso检测文件夹是否存在,存在返回true,不存在返回false
' 参数 :folder(文件夹位置)
'*****************************************************************
private function isfolder(byref folder)
if fso.folderexists(server.mappath(folder)) then
isfolder = true
else
isfolder = false
end if
end function

'*****************************************************************
' function(私有)
' 作用 :利用fso创建文件夹
' 参数 :fldr(文件夹位置)
'*****************************************************************
private function createfolder(byref fldr)
dim f
set f = fso.createfolder(server.mappath(fldr))
createfolder = f.path
set f=nothing
end function

'*****************************************************************
' function(公有)
' 作用 :保存文件,并自动创建多级文件夹
' 参数 :fromurl(远程文件地址),tofiles (保存位置)
'*****************************************************************
public function savedata(byref fromurl,byref tofiles)
tofiles=trim(replace(tofiles,"//","/"))
flname=tofiles
fldr=""
if isexists(flname)=false then
getnewsfold=split(flname,"/")
for i=0 to ubound(getnewsfold)-1
if fldr="" then
fldr=getnewsfold(i)
else
fldr=fldr&"\"&getnewsfold(i)
end if
if isfolder(fldr)=false then
createfolder fldr
end if
next
savefiles fromurl,flname
end if
end function
'*****************************************************************
' function(公有)
' 作用 :取得远程数据
' 参数 :url(远程文件地址),getmode (模式:0为二进制,1为中文编码)
'*****************************************************************
public function getdata(byref url,byref getmode)
'on error resume next
sourcecode = oxml.open ("get",url,false)
oxml.send()
if oxml.readystate<>4 then exit function
if getmode=0 then
getdata = oxml.responsebody
else
getdata = bytestobstr(oxml.responsebody)
end if
if err.number<>0 then err.clear
end function

'*****************************************************************
' function(公有)
' 作用 :格式化远程图片地址为本地位置
' 参数 :imgurl(远程图片地址),imgfolder (本地图片目录),fristname(加入的前缀名称)
'*****************************************************************
public function formatimgpath(byref imgurl,byref imgfolder,byref fristname,byref noimg)
strpath=""
imgurl=imgurl
if instr(imgurl,"nophoto") or lenb(getdata(imgurl,0))<=0 then
strpath=noimg
response.write ""&strpath&"" &vbcrlf
else
if instr(imgurl,".asp") then
strpath=fristname&"_"&mid(imgurl, instrrev(imgurl, "=")+1)&".jpg"
else
strpath=fristname&"_"&mid(imgurl, instrrev(imgurl, "/")+1)
end if
strpath = imgfolder&"/"&strpath
strpath = replace(strpath,"//","/")
if left(strpath,1)="/" then strpath=right(strpath,len(strpath)-1)
strpath = trim(strpath)
response.write ""&strpath&"" &vbcrlf
savedata imgurl,strpath
end if
formatimgpath = strpath
end function