利用ASP下載遠(yuǎn)程文件到服務(wù)器
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
調(diào)用download方法就可以下載文件了,程序會(huì)自動(dòng)獲取文件名,如果獲取失敗會(huì)以dat后綴保存文件;適合小文件的下載[br]
<% [br][br]'函數(shù)名:writfile[br]'作用:把指定二進(jìn)制數(shù)據(jù)寫入文件[br]'參數(shù):byt--二進(jìn)制數(shù)據(jù),file--要寫入的文件名[br]public function writfile(byval byt, byval filename) '寫入文件函數(shù)[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]'函數(shù)名:download[br]'作用:下載文件[br]'參數(shù):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]'函數(shù)名:getname[br]'作用:按日期獲取隨機(jī)數(shù)字[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 編輯過(guò) |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |