• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    asp createTextFile生成文本文件支持utf8

    发布者: 涵韵 | 发布时间: 2025-6-18 08:10| 查看数: 104| 评论数: 0|帖子模式

    但很多时候为了方便,我们会自定义生成文本文件的函数
    1. Function createTextFile(Byval content,Byval fileDir,Byval code)
    2.         dim fileobj,fileCode : fileDir=replace(fileDir, "", "/")
    3.         if isNul(code) then fileCode=Charset else fileCode=code
    4.         call createfolder(fileDir,"filedir")
    5.         if fileCode="utf-8" then
    6.                 on error resume next
    7.                 With objStream
    8.                         .Charset=fileCode:
    9.                         .Type=2:
    10.                         .Mode=3:
    11.                         .Open:
    12.                         .Position=0
    13.                         .WriteText content:
    14.                         .SaveToFile Server.MapPath(fileDir), 2
    15.                         .Close
    16.                 End With
    17.         else
    18.                 on error resume next:err.clear
    19.                 set fileobj=objFso.CreateTextFile(server.mappath(fileDir),True)
    20.                 fileobj.Write(content)
    21.                 set fileobj=nothing
    22.         end if
    23.         if Err Then err.clear :createTextFile=false : errid=err.number:errdes=err.description:Err.Clear : echoErr err_09,errid,errdes else createTextFile=true
    24. End Function
    25. Sub echoErr(byval str,byval id, byval des)

    26. dim errstr,cssstr
    27.                 cssstr="<meta http-equiv=""Content-Type"" content=""text/html; charset="&Charset&""" />"
    28. 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>"
    29. 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>&nbsp;<a href=""" rel="external nofollow" & sitePath &"/"">返回首页</a></div><div id='msgbottom'>Powered by AspCms2.0</div></div>"
    30. cssstr=""
    31. die(errstr)
    32. End Sub
    33. Function createFolder(Byval dir,Byval dirType)
    34. dim subPathArray,lenSubPathArray, pathDeep, i
    35. on error resume next
    36. dir=replace(dir, "", "/")
    37.   if trim(sitePath) = "" then pathDeep = "/" else pathDeep = sitePath
    38.   pathDeep = server.MapPath(pathDeep)
    39. dir=replace(server.mappath(dir), pathDeep, "")
    40. subPathArray=split(dir, "")
    41. select case dirType
    42. case "filedir"
    43. lenSubPathArray=ubound(subPathArray) - 1
    44. case "folderdir"
    45. lenSubPathArray=ubound(subPathArray)
    46. end select
    47. for i=0 to lenSubPathArray
    48.     if trim(subPathArray(i)) <> "" then
    49.   pathDeep=pathDeep&""&subPathArray(i)
    50.   if not objFso.FolderExists(pathDeep) then objFso.CreateFolder pathDeep
    51.     end if
    52. next
    53. if Err Then createFolder=false : errid=err.number:errdes=err.description:Err.Clear : echoErr err_10,errid,errdes else createFolder=true
    54. End Function
    复制代码
    使用方法
    1. createTextFile content,htmlfilepath,""
    复制代码
    content就是内容,filepath就是文件路径
    createTextFile "内容","/article/11/22/3.htm",""
    ASP生成UTF-8编码的代码
    方法一:createtextfile生成文件方法
    1. function WriteToFile(FileName,FileContent)
    2. set fso=server.createobject("scripting.filesystemobject")
    3. set fp=fso.createtextfile(server.mappath(FileName),,True)
    4. fp.write(FileContent)
    5. end function
    复制代码
    方法二:ADODB.Stream生成文件方法 支持utf8,最上面的函数就包括下面的两种方法
    1. Set ccObjStream = Server.CreateObject("ADODB.Stream")
    2. With ccObjStream
    3. .Type = 2
    4. .Mode = 3
    5. .Open
    6. .Charset = "utf-8"
    7. .Position = ccObjStream.Size
    8. .WriteText 要生成的内容
    9. .SaveToFile 要生成文件路径和文件名,2
    10. .Close
    11. 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的资料请关注脚本之家其它相关文章!

    来源:https://www.jb51.net/article/194462.htm
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表