SoFunction
Updated on 2025-03-04

asp createTextFile generates text files and supports utf8

But many times, for convenience, we customize the function that generates text files

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 (fileDir), 2
			.Close
		End With
	else
		on error resume next:
		set fileobj=((fileDir),True)
		(content)
		set fileobj=nothing
	end if
	if Err Then  :createTextFile=false : errid=:errdes=: : 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(){='"& sitePath &"/';}</script><div id='msg'><div class='msgtitle'>hint:【"&str&"】</div><div id='msgbody'>Error number:"&id&"<br>Error description:"&des&"<br /><a href=""javascript:(-1);" rel="external nofollow" ">Return to the previous page</a> <a href=""" rel="external nofollow" & sitePath &"/"">Return to homepage</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 = (pathDeep)
dir=replace((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 (pathDeep) then  pathDeep
    end if
next
if Err Then createFolder=false : errid=:errdes=: : echoErr err_10,errid,errdes else createFolder=true
End Function

How to use

createTextFile content,htmlfilepath,""

content is content, filepath is file path

createTextFile "Content","/article/11/22/",""

ASP generates UTF-8 coded code

Method 1: createtextfile generate file method

function WriteToFile(FileName,FileContent)
set fso=("")
set fp=((FileName),,True)
(FileContent)
end function

Method 2: Generate file method Support utf8, the top function includes the following two methods

Set ccObjStream = ("")
With ccObjStream
.Type = 2
.Mode = 3
.Open
.Charset = "utf-8"
.Position = 
.WriteText Content to be generated
.SaveToFile To generate file path and file name,2
.Close
End With

CreateTextFile method
Creates a specified file and returns a TextStream object that can be used to read or write the created file.

(filename[, overwrite[, unicode]])

parameter
object

Required option. Should be the name of the FileSystemObject or Folder object.

filename

Required option. A string expression that indicates the file to be created.

overwrite

Optional. The Boolean value indicates whether an existing file can be overwritten. If the file can be overwritten, the value is True; if the file cannot be overwritten, the value is False. If this value is omitted, the existing files cannot be overwritten.

unicode

Optional. The Boolean value indicates whether a file is created in Unicode or ASCII file format. This value is True if a file is created in Unicode file format, and False if a file is created in ASCII file format. If this section is omitted, an ASCII file is assumed to be created.

The above is the detailed content of the text file generated by asp createTextFile that supports utf8. For more information about asp createTextFile, please follow my other related articles!