Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
Cómo escribir en un archivo desde una función javascript después de hacer clic en un botón en html

Estoy tratando de escribir un código realmente básico donde, después de presionar un botón en un archivo html, se escribe un archivo .txt localmente a través de una función javascript. ¿Es esto imposible de hacer? Puedo escribir en un archivo solo con el archivo javascript, pero no cuando intento con ambos.

 <!DOCTYPE html> <html> <head> <title>Tic Tac Toe</title> <script type = "text/javascript" src="test.js"></script> </head> <body> <input type="button" onclick="javascript: myFunction()" value="Display"/> </body> </html>
índice.html

 function myFunction() { const fs = require('fs') // Data which will write in a file. let data = "Learning how to write in a file." // Write data in 'Output.txt' . fs.writeFile('output.txt', data, (err) => { // In case of a error throw err. if (err) throw err; }) }

prueba.js

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puede usar File API , que implementa Blob API , así

 const writeToTextFile = (text, fileName) => { let textFile = null; const makeTextFile = (text) => { const data = new Blob([text], { type: 'text/plain', }); if (textFile !== null) { window.URL.revokeObjectURL(textFile); } textFile = window.URL.createObjectURL(data); return textFile; }; const link = document.createElement('a'); link.setAttribute('download', fileName); link.href = makeTextFile(text); link.click(); }; const data = 'Learning how to write in a file.'; writeToTextFile(data, 'output.txt');
about 4 years ago · Juan Pablo Isaza Report

0

Si planea usar el navegador para crear un archivo en un servidor directamente, es de esperar que no sea posible. Imagine que alguien malintencionado envía un script con instrucciones para borrar todo el servidor, por ejemplo.

 const fs = require('fs')

esta es una biblioteca para usar en un lado del servidor, el nodo js crea archivos localmente porque es un motor del lado del servidor.

Entonces, el flujo correcto es, su navegador hace una solicitud a un servidor, en el lado del servidor valida esa solicitud, así que cree un archivo.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!