简体   繁体   中英

How to read(in), print(out) value to text file by Javascript?

I'm working on with my simple server. And i need a little data(number) to be used for my project, but i don't want to use database(cuz i don't like SQL). So is there any way that i could save my data in file text and use data in that file text? 在此处输入图片说明

I want to use Js to print number from roll call program.htm to hr.txt and then i want read number from hr.txt to roll call program.htm which make a perfect data storage.I tested this kind of saving data in my c++ IDE and it worked, but i don't know if it can work on a server. So can you guys help me out?

Here is a sample code for saving the data as a text file and loading in the HTML. You can modify this code and use as per your convenience.

function saveTextAsFile()
{
    var textToSave = document.getElementById("inputTextToSave").value;
    var textToSaveAsBlob = new Blob([textToSave], {type:"text/plain"});
    var textToSaveAsURL = window.URL.createObjectURL(textToSaveAsBlob);
    var fileNameToSaveAs = document.getElementById("inputFileNameToSaveAs").value;

    var downloadLink = document.createElement("a");
    downloadLink.download = fileNameToSaveAs;
    downloadLink.innerHTML = "Download File";
    downloadLink.href = textToSaveAsURL;
    downloadLink.onclick = destroyClickedElement;
    downloadLink.style.display = "none";
    document.body.appendChild(downloadLink);

    downloadLink.click();
}

function destroyClickedElement(event)
{
    document.body.removeChild(event.target);
}

function loadFileAsText()
{
    var fileToLoad = document.getElementById("fileToLoad").files[0];

    var fileReader = new FileReader();
    fileReader.onload = function(fileLoadedEvent) 
    {
        var textFromFileLoaded = fileLoadedEvent.target.result;
        document.getElementById("inputTextToSave").value = textFromFileLoaded;
    };
    fileReader.readAsText(fileToLoad, "UTF-8");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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