但很多时候为了方便,我们会自定义生成文本文件的函数
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='"& sitepath &"/';}</script><div id='msg'><div class='msgtitle'>提示:【"&str&"】</div><div id='msgbody'>错误号:"&id&"<br>错误描述:"&des&"<br /><a href=""javascript:history.go(-1);" rel="external nofollow" ">返回上一页</a> <a href=""" 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
使用方法
createtextfile content,htmlfilepath,""
content就是内容,filepath就是文件路径
createtextfile "内容","/article/11/22/3.htm",""
asp生成utf-8编码的代码
方法一:createtextfile生成文件方法
function writetofile(filename,filecontent)
set fso=server.createobject("scripting.filesystemobject")
set fp=fso.createtextfile(server.mappath(filename),,true)
fp.write(filecontent)
end function
方法二:adodb.stream生成文件方法 支持utf8,最上面的函数就包括下面的两种方法
set ccobjstream = server.createobject("adodb.stream")
with ccobjstream
.type = 2
.mode = 3
.open
.charset = "utf-8"
.position = ccobjstream.size
.writetext 要生成的内容
.savetofile 要生成文件路径和文件名,2
.close
end with
createtextfile 方法
创建指定文件并返回 textstream 对象,该对象可用于读或写创建的文件。
object.createtextfile(filename[, overwrite[, unicode]])
参数
object
必选项。应为 filesystemobject 或 folder 对象的名称。
filename
必选项。字符串表达式,指明要创建的文件。
overwrite
可选项。boolean 值指明是否可以覆盖现有文件。如果可覆盖文件,该值为 true;如果不能覆盖文件,则该值为 false 。如果省略该值,则不能覆盖现有文件。
unicode
可选项。boolean 值指明是否以 unicode 或 ascii 文件格式创建文件。如果以 unicode 文件格式创建文件,则该值为 true;如果以 ascii 文件格式创建文件,则该值为 false。如果省略此部分,则假定创建 ascii 文件。
以上就是asp createtextfile生成文本文件支持utf8的详细内容,更多关于asp createtextfile的资料请关注代码网其它相关文章!
发表评论