ASP中的FSO組件非常強大,如果沒有這個功能FSO,也不知道ASP會變成怎么樣,其實想要學習ASP編程的朋友一定會接觸到與FSO相關的操作,下面錯新技術頻道就為你帶來ASP FSO文件處理函數大全。
復制代碼 代碼如下:
<%
'建立文件夾函數
Function?CreateFolder(strFolder)'參數為相對路徑
????'首選判斷要建立的文件夾是否已經存在
????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)'參數為相對路徑
?strTestFolder?=?Server.Mappath(strFolder)
?Set?objFSO?=?CreateObject("Scripting.FileSystemObject")
?'檢查文件夾是否存在
?If?objFSO.FolderExists(strTestFolder)?Then
??objFSO.DeleteFolder(strTestFolder)
?end?if
?Set?objFSO?=?Nothing
End?function
'創建文本文件
Function?Createtextfile(fileurl,filecontent)'參數為相對路徑和要寫入文件的內容
?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)'參數為相對路徑
?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
'建立圖片文件并保存圖片數據流
Function?Createimage(fileurl,imagecontent)'參數為相對路徑和文件內容
?Set?objStream?=?Server.CreateObject("ADODB.Stream")???'建立ADODB.Stream對象,必須要ADO?2.5以上版本
?objStream.Type?=1???'以二進制模式打開
?objStream.Open
?objstream.write?imagecontent???'將字符串內容寫入緩沖
?objstream.SaveToFile?server.mappath(fileurl),2???'-將緩沖的內容寫入文件
?objstream.Close()'關閉對象
?set?objstream=nothing
End?Function
'遠程獲取文件數據
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?=?"服務器獲取文件內容出錯"?
??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
'獲取圖片數據流
Function?getpic(url)
on?error?resume?next
dim?http
set?http=server.createobject("MSXML2.XMLHTTP")'使用xmlhttp的方法來獲得圖片的內容
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?=?"服務器獲取文件內容出錯"
?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
%>
?
?上文是錯新技術頻道小編為大家介紹的ASP FSO文件處理函數大全,相信大家都有了一定的了解,錯新技術頻道將和你分享更多的知識,讓你在這個行業越來越好。