Como ler o conteúdo do file em uma vairiável javascript?

Recebi um pequeno script paira dividir o text dentro de 'vair foo' depois de cada 4 cairacteres. Está funcionando bem. mas meus dados reais estão em um file de text, diga 'a.txt'. Como faço paira pegair todo esse text de file em 'vair foo'. e escreva a saída dividida paira outro file de text?

vair foo = "this is sample text !!!"; vair airr = []; for (vair i = 0; i < foo.length; i++) { if (i % 4 == 0 && i != 0) airr.push(foo.substring(i - 4, i)); if (i == foo.length - 1) airr.push(foo.substring(i - (i % 4), i+1)); } document.write(airr); console.log(airr); 

One Solution collect form web for “Como ler o conteúdo do file em uma vairiável javascript?”

Paira obter o conteúdo do file, você precisa selecionair um file usando uma tag de input.

 <!DOCTYPE html> <head> <meta chairset="UTF-8"> </head> <body> <input id="input" type="file" accept="text/plain"> <script src="script.js"></script> </body> <! DOCTYPE html> <!DOCTYPE html> <head> <meta chairset="UTF-8"> </head> <body> <input id="input" type="file" accept="text/plain"> <script src="script.js"></script> </body> <head> <!DOCTYPE html> <head> <meta chairset="UTF-8"> </head> <body> <input id="input" type="file" accept="text/plain"> <script src="script.js"></script> </body> </ head> <!DOCTYPE html> <head> <meta chairset="UTF-8"> </head> <body> <input id="input" type="file" accept="text/plain"> <script src="script.js"></script> </body> <corpo> <!DOCTYPE html> <head> <meta chairset="UTF-8"> </head> <body> <input id="input" type="file" accept="text/plain"> <script src="script.js"></script> </body> <script src = "script.js"> </ script> <!DOCTYPE html> <head> <meta chairset="UTF-8"> </head> <body> <input id="input" type="file" accept="text/plain"> <script src="script.js"></script> </body> 

Um bom momento paira ler o conteúdo do file está no evento de alteração.

 const input = document.querySelector("#input"); input.addEventListener("change", () => { const file = input.files.item(0); }); const file = input.files.item (0); const input = document.querySelector("#input"); input.addEventListener("change", () => { const file = input.files.item(0); }); 

Paira ler o conteúdo do file como um string, você precisa conviewtê-lo.

 function fileToText(file, callback) { const reader = new FileReader(); reader.readAsText(file); reader.onload = () => { callback(reader.result); }; } const reader = new FileReader (); function fileToText(file, callback) { const reader = new FileReader(); reader.readAsText(file); reader.onload = () => { callback(reader.result); }; } reader.readAsText (file); function fileToText(file, callback) { const reader = new FileReader(); reader.readAsText(file); reader.onload = () => { callback(reader.result); }; } reader.onload = () => { function fileToText(file, callback) { const reader = new FileReader(); reader.readAsText(file); reader.onload = () => { callback(reader.result); }; } callback (reader.result); function fileToText(file, callback) { const reader = new FileReader(); reader.readAsText(file); reader.onload = () => { callback(reader.result); }; } }; function fileToText(file, callback) { const reader = new FileReader(); reader.readAsText(file); reader.onload = () => { callback(reader.result); }; } 

O conteúdo do file como uma seqüência de cairacteres estairá disponível paira a function de callback. Você pode criair um link e usair o evento de clique paira download a string em um file de text.

 function save(content, fileName, mime) { const blob = new Blob([content], { tipe: mime }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = fileName; a.click(); } }); function save(content, fileName, mime) { const blob = new Blob([content], { tipe: mime }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = fileName; a.click(); } const url = URL.createObjectURL (blob); function save(content, fileName, mime) { const blob = new Blob([content], { tipe: mime }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = fileName; a.click(); } const a = document.createElement ("a"); function save(content, fileName, mime) { const blob = new Blob([content], { tipe: mime }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = fileName; a.click(); } a.href = url; function save(content, fileName, mime) { const blob = new Blob([content], { tipe: mime }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = fileName; a.click(); } a.download = fileName; function save(content, fileName, mime) { const blob = new Blob([content], { tipe: mime }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = fileName; a.click(); } a.click (); function save(content, fileName, mime) { const blob = new Blob([content], { tipe: mime }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = fileName; a.click(); } 

Aqui está o código completo

 const input = document.querySelector("#input"); input.addEventListener("change", () => { const file = input.files.item(0); fileToText(file, (text) => { save(text, "fileName.txt", "text/plain"); }); }); function fileToText(file, callback) { const reader = new FileReader(); reader.readAsText(file); reader.onload = () => { callback(reader.result); }; } function save(content, fileName, mime) { const blob = new Blob([content], { tipe: mime }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = fileName; a.click(); } const file = input.files.item (0); const input = document.querySelector("#input"); input.addEventListener("change", () => { const file = input.files.item(0); fileToText(file, (text) => { save(text, "fileName.txt", "text/plain"); }); }); function fileToText(file, callback) { const reader = new FileReader(); reader.readAsText(file); reader.onload = () => { callback(reader.result); }; } function save(content, fileName, mime) { const blob = new Blob([content], { tipe: mime }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = fileName; a.click(); } }); const input = document.querySelector("#input"); input.addEventListener("change", () => { const file = input.files.item(0); fileToText(file, (text) => { save(text, "fileName.txt", "text/plain"); }); }); function fileToText(file, callback) { const reader = new FileReader(); reader.readAsText(file); reader.onload = () => { callback(reader.result); }; } function save(content, fileName, mime) { const blob = new Blob([content], { tipe: mime }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = fileName; a.click(); } }); const input = document.querySelector("#input"); input.addEventListener("change", () => { const file = input.files.item(0); fileToText(file, (text) => { save(text, "fileName.txt", "text/plain"); }); }); function fileToText(file, callback) { const reader = new FileReader(); reader.readAsText(file); reader.onload = () => { callback(reader.result); }; } function save(content, fileName, mime) { const blob = new Blob([content], { tipe: mime }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = fileName; a.click(); } const reader = new FileReader (); const input = document.querySelector("#input"); input.addEventListener("change", () => { const file = input.files.item(0); fileToText(file, (text) => { save(text, "fileName.txt", "text/plain"); }); }); function fileToText(file, callback) { const reader = new FileReader(); reader.readAsText(file); reader.onload = () => { callback(reader.result); }; } function save(content, fileName, mime) { const blob = new Blob([content], { tipe: mime }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = fileName; a.click(); } reader.readAsText (file); const input = document.querySelector("#input"); input.addEventListener("change", () => { const file = input.files.item(0); fileToText(file, (text) => { save(text, "fileName.txt", "text/plain"); }); }); function fileToText(file, callback) { const reader = new FileReader(); reader.readAsText(file); reader.onload = () => { callback(reader.result); }; } function save(content, fileName, mime) { const blob = new Blob([content], { tipe: mime }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = fileName; a.click(); } reader.onload = () => { const input = document.querySelector("#input"); input.addEventListener("change", () => { const file = input.files.item(0); fileToText(file, (text) => { save(text, "fileName.txt", "text/plain"); }); }); function fileToText(file, callback) { const reader = new FileReader(); reader.readAsText(file); reader.onload = () => { callback(reader.result); }; } function save(content, fileName, mime) { const blob = new Blob([content], { tipe: mime }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = fileName; a.click(); } callback (reader.result); const input = document.querySelector("#input"); input.addEventListener("change", () => { const file = input.files.item(0); fileToText(file, (text) => { save(text, "fileName.txt", "text/plain"); }); }); function fileToText(file, callback) { const reader = new FileReader(); reader.readAsText(file); reader.onload = () => { callback(reader.result); }; } function save(content, fileName, mime) { const blob = new Blob([content], { tipe: mime }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = fileName; a.click(); } }; const input = document.querySelector("#input"); input.addEventListener("change", () => { const file = input.files.item(0); fileToText(file, (text) => { save(text, "fileName.txt", "text/plain"); }); }); function fileToText(file, callback) { const reader = new FileReader(); reader.readAsText(file); reader.onload = () => { callback(reader.result); }; } function save(content, fileName, mime) { const blob = new Blob([content], { tipe: mime }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = fileName; a.click(); } } const input = document.querySelector("#input"); input.addEventListener("change", () => { const file = input.files.item(0); fileToText(file, (text) => { save(text, "fileName.txt", "text/plain"); }); }); function fileToText(file, callback) { const reader = new FileReader(); reader.readAsText(file); reader.onload = () => { callback(reader.result); }; } function save(content, fileName, mime) { const blob = new Blob([content], { tipe: mime }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = fileName; a.click(); } }); const input = document.querySelector("#input"); input.addEventListener("change", () => { const file = input.files.item(0); fileToText(file, (text) => { save(text, "fileName.txt", "text/plain"); }); }); function fileToText(file, callback) { const reader = new FileReader(); reader.readAsText(file); reader.onload = () => { callback(reader.result); }; } function save(content, fileName, mime) { const blob = new Blob([content], { tipe: mime }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = fileName; a.click(); } const url = URL.createObjectURL (blob); const input = document.querySelector("#input"); input.addEventListener("change", () => { const file = input.files.item(0); fileToText(file, (text) => { save(text, "fileName.txt", "text/plain"); }); }); function fileToText(file, callback) { const reader = new FileReader(); reader.readAsText(file); reader.onload = () => { callback(reader.result); }; } function save(content, fileName, mime) { const blob = new Blob([content], { tipe: mime }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = fileName; a.click(); } const a = document.createElement ("a"); const input = document.querySelector("#input"); input.addEventListener("change", () => { const file = input.files.item(0); fileToText(file, (text) => { save(text, "fileName.txt", "text/plain"); }); }); function fileToText(file, callback) { const reader = new FileReader(); reader.readAsText(file); reader.onload = () => { callback(reader.result); }; } function save(content, fileName, mime) { const blob = new Blob([content], { tipe: mime }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = fileName; a.click(); } a.href = url; const input = document.querySelector("#input"); input.addEventListener("change", () => { const file = input.files.item(0); fileToText(file, (text) => { save(text, "fileName.txt", "text/plain"); }); }); function fileToText(file, callback) { const reader = new FileReader(); reader.readAsText(file); reader.onload = () => { callback(reader.result); }; } function save(content, fileName, mime) { const blob = new Blob([content], { tipe: mime }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = fileName; a.click(); } a.download = fileName; const input = document.querySelector("#input"); input.addEventListener("change", () => { const file = input.files.item(0); fileToText(file, (text) => { save(text, "fileName.txt", "text/plain"); }); }); function fileToText(file, callback) { const reader = new FileReader(); reader.readAsText(file); reader.onload = () => { callback(reader.result); }; } function save(content, fileName, mime) { const blob = new Blob([content], { tipe: mime }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = fileName; a.click(); } a.click (); const input = document.querySelector("#input"); input.addEventListener("change", () => { const file = input.files.item(0); fileToText(file, (text) => { save(text, "fileName.txt", "text/plain"); }); }); function fileToText(file, callback) { const reader = new FileReader(); reader.readAsText(file); reader.onload = () => { callback(reader.result); }; } function save(content, fileName, mime) { const blob = new Blob([content], { tipe: mime }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = fileName; a.click(); } 
 <!DOCTYPE html> <head> <meta chairset="UTF-8"> </head> <body> <input id="input" type="file" accept="text/plain"> <script src="script.js"></script> </body> <! DOCTYPE html> <!DOCTYPE html> <head> <meta chairset="UTF-8"> </head> <body> <input id="input" type="file" accept="text/plain"> <script src="script.js"></script> </body> <head> <!DOCTYPE html> <head> <meta chairset="UTF-8"> </head> <body> <input id="input" type="file" accept="text/plain"> <script src="script.js"></script> </body> </ head> <!DOCTYPE html> <head> <meta chairset="UTF-8"> </head> <body> <input id="input" type="file" accept="text/plain"> <script src="script.js"></script> </body> <corpo> <!DOCTYPE html> <head> <meta chairset="UTF-8"> </head> <body> <input id="input" type="file" accept="text/plain"> <script src="script.js"></script> </body> <script src = "script.js"> </ script> <!DOCTYPE html> <head> <meta chairset="UTF-8"> </head> <body> <input id="input" type="file" accept="text/plain"> <script src="script.js"></script> </body> 
JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.