javascript - Classic Asp 如何将生成的 HTML 保存到服务器上的文本文件中?

标签 javascript html asp-classic

有没有办法将当前页面的 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/

相关文章:

javascript - jQuery 外部链接 anchor 动画

javascript - 使用javascript将值相乘,结果显示在下拉列表中

javascript - 如何避免在 native react 中首次渲染或如何仅渲染获取数据避免一次未定义?

html - 在 html/css 中对齐按钮

css - 将 div 元素缩放到窗口大小

asp.net - 同一 IIS 上的经典 ASP 和 ASP.NET 站点

asp-classic - ASP 换行符 -\n?

rest - 证书颁发机构无效或不正确

javascript - 修改方法以完成从数组中删除空字符串的额外逗号

html - 将粘性位置与绝对值一起使用