Classic Asp How to save the generated HTML to a text file on the server?

Is there a way to store the HTML content of the current page in a text file on the server?

Scenario: I have a classic ASP page that generates HTML (report tables and stuff), after creating the entire page, I would like to save its HTML to a file on the server, so I can read it later.

+3


source to share


1 answer


By replacing response.write with f.write , you can save the file to the server instead of sending the file to the browser, where f is a createTextFile object:

<%
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
%>

      

if you want to save your current page to server with human action, you can get all html content with javascript javascript and send it to ASP file to save it to file. Therefore you need to add a javascript function and a form that submits content:



    <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>

      

and this will be the source of the asp page called 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
%>

      

+1


source







All Articles