正文

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

文章摘要

这篇文章介绍了一个在VBScript中创建文本文件的函数`createTextFile`,该函数接受文件内容、文件路径和编码作为参数。函数首先处理文件路径,将其转换为标准格式。接下来,根据编码参数决定写入方式,当编码为UTF-8时,直接使用默认的文本写入方式;否则,调用`CreateTextFile`函数处理。文章还描述了如何处理文件创建过程中的错误,并通过子函数`echoErr`生成错误日志。此外,文章还提供了一个创建目录的函数`createFolder`,用于确保目录存在,并处理了可能的错误。整体内容围绕在VBScript环境中安全、可靠地创建文本文件及其相关错误处理。

 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