asp createTextFile生成文本文件支持utf8(asscii)这都可以?

随心笔谈3年前发布 admin
190 0 0

文章摘要

这篇文章介绍了三个自定义函数及其在文件操作中的应用,重点在于错误处理和路径管理。首先,`createTextFile`函数负责打开或创建文件并写入内容,支持多种编码方式,并在错误时调用`echoErr`函数显示错误提示。其次,`createFolder`函数用于创建文件夹,处理路径替换和错误检查。最后,`echoErr`函数用于显示错误信息,并通过CSS样式美化错误提示,包含错误编号、描述、位置等细节。文章整体围绕自定义函数的实现及其在实际开发中的应用展开,旨在提供一种高效的文件操作解决方案。


Function createTextFile(Byval content,Byval fileDir,Byval code)
dim fileobj,fileCode : fileDir=replace(fileDir, “”, “/”)
if isNul(code) then fileCode=Charset else fileCode=code
call createfolder(fileDir,”filedir”)
if fileCode=”utf-8″ then
on error resume next
With objStream
.Charset=fileCode:
.Type=2:
.Mode=3:
.Open:
.Position=0
.WriteText content:
.SaveToFile Server.MapPath(fileDir), 2
.Close
End With
else
on error resume next:err.clear
set fileobj=objFso.CreateTextFile(server.mappath(fileDir),True)
fileobj.Write(content)
set fileobj=nothing
end if
if Err Then err.clear :createTextFile=false : errid=err.number:errdes=err.description:Err.Clear : echoErr err_09,errid,errdes else createTextFile=true
End Function
Sub echoErr(byval str,byval id, byval des)

dim errstr,cssstr
cssstr=”<meta http-equiv=””Content-Type”” content=””text/html; charset=”&Charset&””” />”
cssstr=cssstr&”<style>body{text-align:center}#msg{background-color:white;border:1px solid #0073B0;margin:0 auto;width:400px;text-align:left}.msgtitle{padding:3px 3px;color:white;font-weight:700;line-height:28px;height30px;font-size:12px;border-bottom:1px solid #0073B0; text-indent:3px; background-color:#0073B0}#msgbody{font-size:12px;padding:20px 8px 30px;line-height:25px}#msgbottom{text-align:center;height:20px;line-height:20px;font-size:12px;background-color:#0073B0;color:#FFFFFF}</style>”
errstr=cssstr&”<script language=””javascript””>setTimeout(“”goLastPage()””,5000);function goLastPage(){location.href=’https://www.jb51.net/article/”& sitePath &”/’;}</script><div id=’msg’><div class=’msgtitle’>提示:【”&str&”】</div><div id=’msgbody’>错误号:”&id&”<br>错误描述:”&des&”<br /><a href=”https://www.jb51.net/article/”javascript:history.go(-1);” rel=”external nofollow” “>返回上一页</a> <a href=”https://www.jb51.net/article/”” rel=”external nofollow” & sitePath &”/””>返回首页</a></div><div id=’msgbottom’>Powered by AspCms2.0</div></div>”
cssstr=””
die(errstr)
End Sub
Function createFolder(Byval dir,Byval dirType)
dim subPathArray,lenSubPathArray, pathDeep, i
on error resume next
dir=replace(dir, “”, “/”)
if trim(sitePath)=”” then pathDeep=”/” else pathDeep=sitePath
pathDeep=server.MapPath(pathDeep)
dir=replace(server.mappath(dir), pathDeep, “”)
subPathArray=split(dir, “”)
select case dirType
case “filedir”
lenSubPathArray=ubound(subPathArray) – 1
case “folderdir”
lenSubPathArray=ubound(subPathArray)
end select
for i=0 to lenSubPathArray
if trim(subPathArray(i)) <> “” then
pathDeep=pathDeep&””&subPathArray(i)
if not objFso.FolderExists(pathDeep) then objFso.CreateFolder pathDeep
end if
next
if Err Then createFolder=false : errid=err.number:errdes=err.description:Err.Clear : echoErr err_10,errid,errdes else createFolder=true
End Function

© 版权声明

相关文章