2017-06-18 149 views
-1

我发现了一个脚本,用于将文本框的内容写入文本文件,并显示下载链接。使用javascript将数据保存到文本文件中

但是现在我需要帮助修改此代码,以便在点击按钮后将文本文件保存在服务器中的特定位置。

下面是代码:

<html> 
<head> 
    <script type='text/javascript'>//<![CDATA[ 
     window.onload=function(){ 
      (function() { 
       var textFile = null, 
       makeTextFile = function (text) { 
        var data = new Blob([text], {type: 'text/plain'}); 
        // If we are replacing a previously generated file we need to 
        // manually revoke the object URL to avoid memory leaks. 
        if (textFile !== null) { 
         window.URL.revokeObjectURL(textFile); 
        } 
        textFile = window.URL.createObjectURL(data); 
        return textFile; 
       }; 

       var create = document.getElementById('create'), 
        textbox = document.getElementById('textbox'); 

       create.addEventListener('click', function() { 
        var link = document.getElementById('downloadlink'); 
        link.href = makeTextFile(textbox.value); 
        link.style.display = 'block'; 
       }, false); 
      })(); 
     }//]]> 
     </script> 
</head> 
<body> 
    <textarea id="textbox">Type something here</textarea> 
    <button id="create">Create file</button> 
    <a download="info.txt" id="downloadlink" style="display: none">Download</a> 

    <script> 
    // tell the embed parent frame the height of the content 
    if (window.parent && window.parent.parent){ 
     window.parent.parent.postMessage(["resultsFrame", { 
      height: document.body.getBoundingClientRect().height, 
      slug: "qm5AG" 
     }], "*") 
    } 
    </script> 
</body> 
</html> 

请指教。

+0

您应该将文本发送到服务器,并在您的后端代码上执行您想要的操作。 – Gerardo

回答

2

您将需要了解服务器端实践的后端开发。客户端无法对服务器进行任何修改而无需使用后端。 (这将是一个巨大的安全漏洞!)我建议在这里学习NodeJS:https://www.w3schools.com/nodejs/

一旦你变得更加熟悉了,并且认为你已经准备好再次拿起这个项目,给我一个直接的信息,我会指出你在正确的方向。

相关问题