有没有办法将当前页面的 HTML 内容保存到服务器上的文本文件中?
场景: 我有一个生成 HTML(报告表和其他东西)的经典 ASP 页面,在生成整个页面后,我想将它的 HTML 代码保存到服务器上的一个文件中,以便稍后阅读。
最佳答案
通过将response.write替换为f.write,您可以将文件保存在服务器上而不是将文件发送到浏览器,其中f 是 createTextFile 对象:
<%
dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.CreateTextFile("c:\test.txt",true)
f.write("Your current html and asp codes goes here considering syntax which connects html strings and asp codes in classic asp")
f.close
set f=nothing
set fs=nothing
%>
如果您想通过人工操作将当前页面保存到服务器中,您可以使用 (javascript) jquery 获取整个 html 内容并将其发送到 ASP 文件以将其保存在文件中。所以你必须添加一个 javascript 函数和一个发送内容的表单:
<html>
<!--your entire page is here-->
</html>
<!--You should add these extra code at the end of html file:-->
<a onclick="submitform();">Save the page by clicking here</a>
<form id="myform" action="savefile.asp" method="post">
<textarea name="body"></textarea>
</form>
<script type="text/javascript">
// assuming you have included jquery in your project:
function getPageHTML() {
return "<html>" + $("html").html() + "</html>" // You can also add doctype or other content out of html tags as you need;
}
function submitform(){
var content=getPageHTML();
$("#mytextarea").val(content);
$("#myform").submit()
}
</script>
这将是 asp 页面命名的来源 savefile.asp :
<%
dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.CreateTextFile (server.mapath("test.txt"),true)
dim body
body=request.form("body")
body=replace(body,chr(34),chr(34)&chr(34)) 'To escpase double quotes
f.write (body)
f.close
set f=nothing
set fs=nothing
%>
关于javascript - Classic Asp 如何将生成的 HTML 保存到服务器上的文本文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29854063/