由asp如何生成html或htm

tickon 2004-08-10 08:06:00
大于256K的asp文件如何自动生成html或htm
...全文
403 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
tickon 2004-08-18
  • 打赏
  • 举报
回复
to jzywh(江小鱼@湖北大学):
谢谢 明天收贴
jzywh 2004-08-16
  • 打赏
  • 举报
回复
有两种主要的文件处理类型:

创建、添加或删除数据,以及读取文件
移动、复制和删除文件
创建文件
创建空文本文件(有时被叫做“文本流”)有三种方法。

第一种方法是用 CreateTextFile 方法。下面的示例示范了如何用 CreateTextFile 方法创建文本文件:

[VBScript]
Dim fso, f1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile("c:\testfile.txt", True)
[JScript]
var fso, f1;
fso = new ActiveXObject("Scripting.FileSystemObject");
f1 = fso.CreateTextFile("c:\\testfile.txt", true);
创建文本文件的第二种方法是,使用 FileSystemObject 对象的 OpenTextFile 方法,并设置 ForWriting 标志。

[VBScript]
Dim fso, ts
Const ForWriting = 2
Set fso = CreateObject("Scripting. FileSystemObject")
Set ts = fso.OpenTextFile("c:\test.txt", ForWriting, True)
[JScript]
var fso, ts;
var ForWriting= 2;
fso = new ActiveXObject("Scripting.FileSystemObject");
ts = fso.OpenTextFile("c:\\test.txt", ForWriting, true);
创建文本文件的第三种方法是,使用 OpenAsTextStream 方法,并设置 ForWriting 标志。要使用这种方法,使用下面的代码:

[VBScript]
Dim fso, f1, ts
Const ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile ("c:\test1.txt")
Set f1 = fso.GetFile("c:\test1.txt")
Set ts = f1.OpenAsTextStream(ForWriting, True)
[JScript]
var fso, f1, ts;
var ForWriting = 2;
fso = new ActiveXObject("Scripting.FileSystemObject");
fso.CreateTextFile ("c:\\test1.txt");
f1 = fso.GetFile("c:\\test1.txt");
ts = f1.OpenAsTextStream(ForWriting, true);
添加数据到文件中
一旦创建了文本文件,使用下面的三个步骤向文件添加数据:

打开文本文件。

写入数据。

关闭文件。

要打开现有的文件,则使用 FileSystemObject 对象的 OpenTextFile 方法或 File 对象的 OpenAsTextStream 方法。

要写数据到打开的文本文件,则根据下表所述任务使用 TextStream 对象的 Write、WriteLine 或 WriteBlankLines 方法。

任务 方法
向打开的文本文件写数据,不用后续一个新行字符。 Write
向打开的文本文件写数据,后续一个新行字符。 WriteLine
向打开的文本文件写一个或多个空白行。 WriteBlankLines

要关闭一个打开的文件,则使用 TextStream 对象的 Close 方法。

注意 新行字符包含一个或几个字符(取决于操作系统),以把光标移动到下一行的开始位置(回车/换行)。注意某些字符串末尾可能已经有这个非打印字符了。
下面的例子示范了如何打开文件,和同时使用三种写方法来向文件添加数据,然后关闭文件:

[VBScript]
Sub CreateFile()
Dim fso, tf
Set fso = CreateObject("Scripting.FileSystemObject")
Set tf = fso.CreateTextFile("c:\testfile.txt", True)
' 写一行,并带有一个新行字符。
tf.WriteLine("Testing 1, 2, 3.")
' 向文件写三个新行字符。
tf.WriteBlankLines(3)
' 写一行。
tf.Write ("This is a test.")
tf.Close
End Sub
[JScript]
function CreateFile()
{
var fso, tf;
fso = new ActiveXObject("Scripting.FileSystemObject");
tf = fso.CreateTextFile("c:\\testfile.txt", true);
// 写一行,并带有一个新行字符。
tf.WriteLine("Testing 1, 2, 3.") ;
// 向文件写三个新行字符。
tf.WriteBlankLines(3) ;
// 写一行。
tf.Write ("This is a test.");
tf.Close();
}
读取文件
要从文本文件读取数据,则使用 TextStream 对象的 Read、ReadLine 或 ReadAll 方法。下表描述了不同的任务应使用哪种方法。

任务 方法
从文件读取指定数量的字符。 Read
读取一整行(一直到但不包括新行字符)。 ReadLine
读取文本文件的整个内容。 ReadAll

如果使用 Read 或 ReadLine 方法,并且想跳过数据的特殊部分,则使用 Skip 或 SkipLine 方法。read 方法的结果文本存在一个字符串中,该字符串可以显示在一个控件中,也可以用字符串函数(如 Left、Right 和 Mid)来分析,连接等等。

下面的例子示范了如何打开文件,和如何写数据到文件中并从文件读取数据:

[VBScript]
Sub ReadFiles
Dim fso, f1, ts, s
Const ForReading = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile("c:\testfile.txt", True)
' 写入一行。
Response.Write "Writing file <br>"
f1.WriteLine "Hello World"
f1.WriteBlankLines(1)
f1.Close
' 读取文件内容。
Response.Write "Reading file <br>"
Set ts = fso.OpenTextFile("c:\testfile.txt", ForReading)
s = ts.ReadLine
Response.Write "File contents = '" & s & "'"
ts.Close
End Sub
[JScript]
function ReadFiles()
{
var fso, f1, ts, s;
var ForReading = 1;
fso = new ActiveXObject("Scripting.FileSystemObject");
f1 = fso.CreateTextFile("c:\\testfile.txt", true);
// 写入一行。
Response.Write("Writing file <br>");
f1.WriteLine("Hello World");
f1.WriteBlankLines(1);
f1.Close();
// 读取文件内容。
Response.Write("Reading file <br>");
ts = fso.OpenTextFile("c:\\testfile.txt", ForReading);
s = ts.ReadLine();
Response.Write("File contents = '" + s + "'");
ts.Close();
}
移动、复制和删除文件
FSO 对象模型各有两种方法移动、复制和删除文件,如下表所述。

任务 方法
移动文件 File.Move 或 FileSystemObject.MoveFile
复制文件 File.Copy 或 FileSystemObject.CopyFile
删除文件 File.Delete 或 FileSystemObject.DeleteFile

下面示例在驱动器 C 的根目录中创建一个文本文件,向其中写一些信息,然后把它移动到 \tmp 目录中,并在 \temp 中做一个备份,最后把它们从两个目录中删掉。

要运行下面的示例,需要先在驱动器 C 的根目录中创建 \tmp 和 \temp 目录:

[VBScript]
Sub ManipFiles
Dim fso, f1, f2, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile("c:\testfile.txt", True)
Response.Write "Writing file <br>"
' 写入一行。
f1.Write ("This is a test.")
' 关闭写入到的文件。
f1.Close
Response.Write "Moving file to c:\tmp <br>"
' 获取到 C:\ 根目录中文件的句柄。
Set f2 = fso.GetFile("c:\testfile.txt")
' 将文件移到 \tmp 目录。
f2.Move ("c:\tmp\testfile.txt")
Response.Write "Copying file to c:\temp <br>"
' 将文件复制到 \temp。
f2.Copy ("c:\temp\testfile.txt")
Response.Write "Deleting files <br>"
' 获得文件当前位置的句柄。
Set f2 = fso.GetFile("c:\tmp\testfile.txt")
Set f3 = fso.GetFile("c:\temp\testfile.txt")
' 删除文件。
f2.Delete
f3.Delete
Response.Write "All done!"
End Sub
[JScript]
function ManipFiles()
{
var fso, f1, f2, s;
fso = new ActiveXObject("Scripting.FileSystemObject");
f1 = fso.CreateTextFile("c:\\testfile.txt", true);
Response.Write("Writing file <br>");
// 写入一行。
f1.Write("This is a test.");
// 关闭写入到的文件。
f1.Close();
Response.Write("Moving file to c:\\tmp <br>");
// 获取到 C:\ 根目录中文件的句柄。
f2 = fso.GetFile("c:\\testfile.txt");
// 将文件移到 \tmp 目录。
f2.Move ("c:\\tmp\\testfile.txt");
Response.Write("Copying file to c:\\temp <br>");
// 将文件复制到 \temp。
f2.Copy ("c:\\temp\\testfile.txt");
Response.Write("Deleting files <br>");
// 获得文件当前位置的句柄。
f2 = fso.GetFile("c:\\tmp\\testfile.txt");
f3 = fso.GetFile("c:\\temp\\testfile.txt");
// 删除文件。
f2.Delete();
f3.Delete();
Response.Write("All done!");
}

tickon 2004-08-16
  • 打赏
  • 举报
回复
up
tickon 2004-08-16
  • 打赏
  • 举报
回复
没有人愿要吗
tickon 2004-08-15
  • 打赏
  • 举报
回复
我的asp生成htm后大小超过256K 谁给我一个js的具体例子,这50分就给他了
AVAmyZ 2004-08-11
  • 打赏
  • 举报
回复
js的方式试试!》?
BlueDestiny 2004-08-11
  • 打赏
  • 举报
回复
<%
Dim fso,MyFile
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("e:\Sfqh_Websites\1.htm", True)
MyFile.WriteLine("生成.htm的文件!")
MyFile.close

%>
tickon 2004-08-11
  • 打赏
  • 举报
回复
哪位大虾有例子
aifox 2004-08-10
  • 打赏
  • 举报
回复
to ydh1981

你的这个方案只能转化固定的文件
能不能做到可以选择呢
我像这样做
但是发现<!---->标记种插不进变量阿
有什么办法啊
用fso把文件都写出来行不行
幻影时空 2004-08-10
  • 打赏
  • 举报
回复
对哟,生成HTML还是生成ASP都是用FSO来生成的!(前提:空间要支付FSO)
ycted 2004-08-10
  • 打赏
  • 举报
回复
一般来说都是用fso生成的html文件.
ydh1981 2004-08-10
  • 打赏
  • 举报
回复

一个完整的例子,很好用的.呵呵


<%
filename="index.htm"
MDBpath="./"
if request("body")<>"" then
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set fout = fso.CreateTextFile(server.mappath(""&filename&""))
fout.Write request("body")
fout.close
set fout=nothing
set fso=nothing
mskrindex="ok"
end if
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>程序</title>
</head>
<body topmargin="1" leftmargin="1">
<%if mskrindex="ok" then%>
<script language=javascript>

window.close();
</script>
<%else%>
<table border="0" cellspacing="1" style="border-collapse: collapse" width="100%" bgcolor="#555555" height="100%">
<form name="frmAnnounce" method="post" action="makeindex.asp">
<tr>
<td width="100%" bgcolor="#6A6A6A">
<b><font color="#FFFFFF">生成首页 生成文件</font>:<a class=JyDownSort target="_blank" href="<%=filename%>"><%=filename%></a> ,<font color="#FFFFFF">源文件</font>:<a class=JyDownSort target="_blank" href="index.asp">index.asp</a></b></td>
</tr>
<tr>
<td width="100%" bgcolor="#FFFFFF" height="100%"><textarea style="width:100%; height:100%;" rows="19" name="body" cols="102"><!--#include file="index.asp"--></textarea></td>
</tr>
<tr>
<td width="100%" bgcolor="#DFDFDF">
<p align="center">
<input name="change" class=buttonface value="生成首页(HTML)" type='submit'></td>
</tr>
</form>
</table>
<script language=javascript>
document.frmAnnounce.submit();
</script>
<%end if%>
</body>
</html>

xzq686 2004-08-10
  • 打赏
  • 举报
回复
ASP文件生成html文件?
具体,你自己试试。。
下面有一段代码你看看。这个经常用在网页上的word或excel文档链接怎么让它点击提示下载而不是直接打开。
下面的代码会提示你把ASP执行的结果保存成Test.htm文件。具体的方法是:
<%
Response.ContentType = "APPLICATION/OCTET-STREAM"
Response.AddHeader "Content-Disposition","attachment;filename=Test.htm"
Response.write "<div style=''background-color:navy;color:#FFFFFF''>测试</div>"
Response.write "<a href=''http://lucky.myrice.com''>"
Response.write "<img src=''http://lucky.myrice.com/back.jpg''>【孟宪会之精彩世界】</a>"
Response.End
%>
wwweasy 2004-08-10
  • 打赏
  • 举报
回复
asp文件只能用IIS能工具解析,解析后只能看到htm语言,看不到asp语句,所以如果改扩展名后只有htl语句能用,asp语句就不能用。
emilsong 2004-08-10
  • 打赏
  • 举报
回复
ASP直接生成JS,然后调用JS,例如:
Function makejs()
Dim sql,rs,tempstr
sql="select Name From book"
set rs=conn.execute(sql)
tempstr="<table>"
do while not rs.eof
tempstr=tempstr&"<tr><td>"&rs(0)&"</td></tr>"
rs.movenext
loop
tempstr=tempstr&"</table>"
makejs="document.write('"&tempstr&"')"
End Function

在前台的HTML叶面中直接调用<script src="*.asp"></script>
海大富运动 2004-08-10
  • 打赏
  • 举报
回复
mark,study
chhwang 2004-08-10
  • 打赏
  • 举报
回复
写个DLL组件再调用生成HTM
tickon 2004-08-10
  • 打赏
  • 举报
回复
to ydb1981:
你的方法能解决256k以内的asp生成htm
 256k以上的就不行。
 谁还有高招吗?

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧