利用ASP下载远程文件到服务器
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
调用download方法就可以下载文件了,程序会自动获取文件名,如果获取失败会以dat后缀保存文件;适合小文件的下载[br]
<% [br][br]'函数名:writfile[br]'作用:把指定二进制数据写入文件[br]'参数:byt--二进制数据,file--要写入的文件名[br]public function writfile(byval byt, byval filename) '写入文件函数[br]on error resume next[br] dim objaso:set objaso=server.createobject("adodb.stream")[br] objaso.type = 1[br] objaso.mode = 3[br] objaso.open[br] objaso.position = 0[br] objaso.write byt[br] objaso.savetofile filename, 2[br] objaso.close[br] set objaso = nothing[br] writfile = true[br]end function[br][br]'函数名:download[br]'作用:下载文件[br]'参数:url-要获取的url,savepath为文件保存地址[br]public function download(byval url, byval savepath)[br]on error resume next[br]dim resbody, sstr, vpath, filename, verr[br]verr = true[br]vpath = replace(savepath, "/", "\")[br]if right(vpath, 1) <> "\" then vpath = vpath & "\"[br]spos = instrrev(url, "/") + 1[br]sstr = mid(url, spos)[br]set http = server.createobject("microsoft.xmlhttp")[br]http.open "get", url, false[br]http.send[br]if http.readystate = 4 then[br] if http.status = 200 then[br] resbody = http.responsebody[br] head = http.getresponseheader("content-disposition")[br] if head <> "" then[br] startpos = instr(head, "=") + 1[br] filename = mid(head, startpos)[br] elseif instr(sstr, ".") > 0 and instr(sstr, "?") <= 0 then[br] filename = sstr[br] else[br] filename = getname() & ".dat"[br] end if[br] if writfile(resbody, vpath & filename) then verr = false[br] end if[br]end if[br]download = not verr[br]end function[br][br]'函数名:getname[br]'作用:按日期获取随机数字[br]public function getname()[br]on error resume next[br] dim y,m,d,h,mm,s, r[br] randomize[br] y = year(now)[br] m = month(now): if m < 10 then m = "0" & m[br] d = day(now): if d < 10 then d = "0" & d[br] h = hour(now): if h < 10 then h = "0" & h[br] mm = minute(now): if mm < 10 then mm = "0" & mm[br] s = second(now): if s < 10 then s = "0" & s[br] r = 0[br] r = cint(rnd() * 1000)[br] if r < 10 then r = "00" & r[br] if r < 100 and r >= 10 then r = "0" & r[br] getname = y & m & d & h & mm & s & r[br]end function[br][br]call download("http://chat.ii-home.cn/down/objupload.rar",server.mappath("."))[br]%> 该文章在 2010/7/8 13:32:48 编辑过 |
关键字查询
相关文章
正在查询... |