save to a data file via keyboard in Avascript

Asked

Viewed 100 times

-3

if possible help me with my doubt, I am more lost than blind in shooting,

look I would like to do a search that the user type himself via prompt and the same was adding, at the end I would like to save in file for example in text so that I can have this data saved and not get lost, since what this variable will be lost in future interactions, such as opening and closing the program.

follows below the code, this very basic, I will still put a command DO WHILE.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">



<script>
var otima=0
var boa=0
var pessima=0
var regular=0
var op=prompt("Escolha opção:")

switch(op){

    case '1': otima=otima+1

            break
    case '2': boa=boa+1

            break
    case '3': pessima=pessima+1

            break

    case '4': resular+regular+1

            break
}

</script>
</head>

<body>

</body>
</htm> 

1

  • But you will do it in the browser or on Node.js?

  • Diego, could you clarify the context of how this will be used? Will integrate with any application that already exists?

  • Good afternoon, it will not be used only in the same browser, in short, via prompt enter the data it, sum and the purpose that the data can be file in a doc.txt.

2 answers

1


Your question is not very clear, It is not giving to understand if you want to save the data to use on the page at another time, for example: when returning to access the page, or if you really want to download a file to for example generate a 'report'.

If you want to save the data to use them at another time on the page you can do as shown by @user1331413 using sessionStorage or localStorage.

Now if you want to download a file one of the ways can be generate a dataURI, for this I will use the function btoa to convert the string to Base64 as shown below.

// String que sera salva.
let texto = "Vou salvar esse texto como arquivo de texto, teste de acentos áãõíçô";

// Gerar link de download dataURI
let link = "data: text; base64,"+btoa(texto);
// tipo do arquivo ^             ^ gerar base64

// Adicionar link na pagina, para download
document.body.innerHTML += "<a href='"+link+"' download='MeuArquivo.txt'>Baixar arquivo</a>";
 // Indica que é para baixar e dá um nome para o arquivo ^

document.body.innerHTML += "<br/><a href='"+link+"'>Abrir link</a>";

0

You can use sessionStorage or localStorage.

Very simple examples.

sessionStorage

let resultado = [{"otima":1}, {"boa":2}, {"pessima":2}, {"regular":2}]

// Grava
sessionStorage.setItem("resultado", JSON.stringify(resultado));

//Le
let resultadoSession = JSON.parse(sessionStorage.getItem("resultado"));

localStorage

let resultado = [{"otima":1}, {"boa":2}, {"pessima":2}, {"regular":2}]

//grava
localStorage.setItem("resultado", JSON.stringify(resultado));

// Le
let resultadoSession = JSON.parse(localStorage.getItem("resultado"));

The basic difference between sessionStorage and localStorage is that the first expires whenever the browser is closed, the second is maintained.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.