简体   繁体   English

如何使用javascript将html保存到服务器文件夹中的.html文件

[英]How to save html to .html file in folder on server with javascript

I'm writing a tool for my website like an admin panel where you can add some elements to the page.我正在为我的网站编写一个工具,例如管理面板,您可以在其中向页面添加一些元素。 Using javascript I add elements and then if save button is pressed everything should save automatically like "smth.html" to the same folder where the js script is.使用 javascript 我添加元素,然后如果按下保存按钮,所有内容都应该像“smth.html”一样自动保存到 js 脚本所在的同一文件夹中。

I've spent a lot of time searching right scripts for it but nothing works我花了很多时间为它搜索正确的脚本,但没有任何效果

Can you advise me some solution to this problem, please?你能告诉我一些解决这个问题的方法吗? Is it possible to do it with code below?可以用下面的代码来做吗? Code bellow helps to download it from webpage, but I need to save it to the server with script下面的代码有助于从网页下载它,但我需要用脚本将它保存到服务器

thanks for your help!感谢您的帮助!


<div class="row form-group">
   <div class="col-md-12">
      <input type="button" onclick="addCard()" id="addBtn" value="Добавить"
          class="btn btn-primary py-2 px-4 text-white btn-lg">
      <input type="button" onclick="saveHtml()" value="Сохранить"
          class="btn btn-primary py-2 px-4 text-white btn-lg">
   </div>
</div>


function saveHtml() {
    var html = document.querySelector("html").innerHTML;

    download("saved.html", html);
}


function download(filename, text) {
        var element = document.createElement('a');
        element.setAttribute('href', 'data:text/plain;charset=utf-8,' +
            encodeURIComponent(text));
        element.setAttribute('download', filename);

        element.style.display = 'none';
        document.body.appendChild(element);

        element.click();

        document.body.removeChild(element);
    }

There is a working code needs only the server side PHP file called file_saver.php where you will receive the POST Data ( innerHTML content is encoded to base64 ) you can just use this code : $html=base64_decode(urldecode($_POST['innerHTML'])); $filename=trim($_POST['filename']); file_put_contents($filename, $html);有一个工作代码只需要名为file_saver.php的服务器端PHP file ,您将在其中接收POST Data (innerHTML 内容被编码为 base64),您可以使用此代码: $html=base64_decode(urldecode($_POST['innerHTML'])); $filename=trim($_POST['filename']); file_put_contents($filename, $html); $html=base64_decode(urldecode($_POST['innerHTML'])); $filename=trim($_POST['filename']); file_put_contents($filename, $html);

Also don't forget to change folder RULES you want to write to on server: chmod 777 /var/www/html另外不要忘记在服务器上更改您要写入的文件夹规则: chmod 777 /var/www/html

 function saveHtml() { let html = document.documentElement; download("saved.html", html); } function download(filename, contentElement){ let fileSaverPath="./file_saver.php"; var xhr = new XMLHttpRequest(); xhr.open("POST", fileSaverPath, true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function() { if (this.readyState === XMLHttpRequest.DONE && this.status === 200) { window.alert("file saved !"); } } let encodedHtml=contentElement.innerHTML.toString(); xhr.send("filename="+filename+"&innerHTML="+btoa(unescape(encodeURIComponent(encodedHtml)))); //document.documentElement.innerHTML=encodedHtml; }
 <div class="row form-group"> <div class="col-md-12"> <input type="button" onclick="addCard()" id="addBtn" value="Добавить" class="btn btn-primary py-2 px-4 text-white btn-lg"> <input type="button" onclick="saveHtml()" value="Сохранить" class="btn btn-primary py-2 px-4 text-white btn-lg"> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM