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

177
June 09, 2019, at 2:40 PM

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?

Answer 1

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");
}
Rent Charter Buses Company
READ ALSO
Implementing “AddToCard” in React.js ecommerce project

Implementing “AddToCard” in React.js ecommerce project

I have been building an e-commerce websiteI have an issue after adding items to the shopping card

134
How to create a veutify superset to extend basic functionality

How to create a veutify superset to extend basic functionality

I'm trying to create a plugin based component library to provide consistency across multiple product lines using veutifyHowever, install the library and add the components I get several errors regarding dark theme

182
How to fix the Restart button in the game?

How to fix the Restart button in the game?

When pressing on the Restart at anytime some problems happen like the moves counter become unexpected sometimes add 2 and other 3 instead of adding only one , also the cards become in matched mode instead of opening and comparing mode : [its clear in the link][1]

132