asp 中處理文件上傳以及刪除時常用的自定義函數(shù):
<% '建立文件夾函數(shù) Function CreateFolder(strFolder)'參數(shù)為相對路徑 '首選判斷要建立的文件夾是否已經(jīng)存在 Dim strTestFolder,objFSO strTestFolder = Server.Mappath(strFolder) Set objFSO = CreateObject("Scripting.FileSystemObject") '檢查文件夾是否存在 If not objFSO.FolderExists(strTestFolder) Then '如果不存在則建立文件夾 objFSO.CreateFolder(strTestFolder) End If Set objFSO = Nothing End function '刪除文件夾 Function DelFolder(strFolder)'參數(shù)為相對路徑 strTestFolder = Server.Mappath(strFolder) Set objFSO = CreateObject("Scripting.FileSystemObject") '檢查文件夾是否存在 If objFSO.FolderExists(strTestFolder) Then objFSO.DeleteFolder(strTestFolder) end if Set objFSO = Nothing End function '創(chuàng)建文本文件 Function Createtextfile(fileurl,filecontent)'參數(shù)為相對路徑和要寫入文件的內(nèi)容 Set objFSO = Server.CreateObject("Scripting.FileSystemObject") Set fout = objFSO.CreateTextFile(Server.MapPath(fileurl)) fout.WriteLine filecontent fout.close Set objFSO = Nothing End Function '刪除文件(適合所有文件) Function Deltextfile(fileurl)'參數(shù)為相對路徑 Set objFSO = CreateObject("Scripting.FileSystemObject") fileurl = Server.MapPath(fileurl) if objFSO.FileExists(fileurl) then '檢查文件是否存在 objFSO.DeleteFile(Server.mappath(fileurl)) end if Set objFSO = nothing End Function '建立圖片文件并保存圖片數(shù)據(jù)流 Function Createimage(fileurl,imagecontent)'參數(shù)為相對路徑和文件內(nèi)容 Set objStream = Server.CreateObject("ADODB.Stream") '建立ADODB.Stream對象,必須要ADO 2.5以上版本 objStream.Type =1 '以二進(jìn)制模式打開 objStream.Open objstream.write imagecontent '將字符串內(nèi)容寫入緩沖 objstream.SaveToFile server.mappath(fileurl),2 '-將緩沖的內(nèi)容寫入文件 objstream.Close()'關(guān)閉對象 set objstream=nothing End Function '遠(yuǎn)程獲取文件數(shù)據(jù) Function getHTTPPage(url) 'On Error Resume Next dim http set http=Server.createobject("Microsoft.XMLHTTP") Http.open "GET",url,false Http.send() if Http.readystate<>4 then exit function end if getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") set http=nothing If Err.number<>0 then getHTTPPage = "服務(wù)器獲取文件內(nèi)容出錯" Err.Clear End If End function Function BytesToBstr(body,Cset) dim objstream set objstream = Server.CreateObject("adodb.stream") objstream.Type = 1 objstream.Mode =3 objstream.Open objstream.Write body objstream.Position = 0 objstream.Type = 2 objstream.Charset = Cset BytesToBstr = objstream.ReadText objstream.Close set objstream = nothing End Function '獲取圖片數(shù)據(jù)流 Function getpic(url) on error resume next dim http set http=server.createobject("MSXML2.XMLHTTP")'使用xmlhttp的方法來獲得圖片的內(nèi)容 Http.open "GET",url,false Http.send() if Http.readystate<>4 then exit function end if getpic=Http.responseBody set http=nothing if err.number<>0 then getpic = "服務(wù)器獲取文件內(nèi)容出錯" err.Clear End if End Function '打開文件(文本形式) Function OpenFile(fileurl)'文件相對路徑 Dim Filename,fso,hndFile Filename = fileurl Filename = Server.MapPath(Filename) Set objfso = CreateObject("Scripting.FileSystemObject") If objfso.FileExists(Filename) Then set hndFile = objfso.OpenTextFile(Filename) OpenFile = hndFile.ReadAll Else OpenFile = "文件讀取錯誤" End If Set hndFile = Nothing Set objfso = Nothing End Function '獲得文件的后綴名 function getFileExtName(fileName) dim pos pos=instrrev(filename,".") if pos>0 then getFileExtName=mid(fileName,pos+1) else getFileExtName="" end if end function %>
|