我想使用JavaScript将文本框保存为HTML中的.txt文件,但出现错误

时间:2018-10-13 10:57:08

标签: javascript html

我有一个用于学校项目的虚假网站,我想获取HTML代码,以使用Javascript将信息从文本框发送到.txt文件,但是它不起作用,我也不知道为什么。你能告诉我错误在哪里吗?

<section>
    <form method:"POST">
        <label>Escrigui el seu usuari:</label>
        <input type="text" name="usuari" id="usuari" size="20"/>
        <label>Escrigui la seva contrasenya:</label>
        <input type="text" name="contrasenya" id="contrasenya" size="20"/>
        <input type="button" value="submit" id="button" href="" onclick="WriteToFile()"/>
    </form>

<script type="text/javascript">
    function WriteToFile(passForm){

         set fso = CreateObject("Scripting.FileSystemObject");
         set s   = fso.CreateTextFile("filename.txt", True);

         var usuari = document.getElementById("usuari").value;
         var contrasenya  = document.getElementById("contrasenya").value;

        s.writeline("usuari :" + usuari);
        s.writeline("contrasenya :" + contrasenya);

        s.writeline("-----------------------------");
        s.Close();
        }
</script>   

1 个答案:

答案 0 :(得分:0)

我编写了一个“笔记本”,将输入导出到txt文件,该文件可以在以下时间下载:

<html><head>
<style>
    body {
        margin: 0%;
    }

    @font-face {
        font-family: "CG";
        src: url(./resources/CENTURYGOTHIC.ttf) format("truetype");
    }

    textarea {
        margin-left: 0%;
        margin-top: 0%;
        height: 80%;
        width: 100%;
        resize: none;
        visibility: visible;
        background-color: white;
        border-color: black;

    }

    button {
        background-color: white;
        font-family: CG;
        border-color: black;
        border-width: 1px;
        padding-top: 10px;
        padding-bottom: 10px;
        padding-right: 20px;
        padding-left: 20px;
        margin-top: 10px;
        text-transform: capitalize;
    }

</style></head>


<body>
    <textarea id="textbox" placeholder="You can write anything here... just start tyipng what you would like to remember: Notes, Homework, Tests, just anything."></textarea>
    <button id="create">SAVE TO FILE</button>
    <a download="notes.txt" id="downloadlink" style="display: none"><button onclick="functionreset()">DOWNLOAD</button></a>
    <br>

    <script>
        (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';
                document.getElementById('create').style.display = 'none';
            }, false);
        })();

        function functionreset() {
            var link = document.getElementById('downloadlink');
            link.style.display = 'none';
            document.getElementById('create').style.display = 'block';
        }

    </script>
    <script>
         document.getElementById("textbox").onchange = function() {functionreset()}
    </script>

<!-- <iframe height=100% width=100%; src=./test.html></iframe>-->
</body></html>

也许您可以使用此片段...

通知我是否有效