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

204
Views
cómo hacer un archivo cuando presiono el botón Enviar en un archivo html con NodeJS

cuando presiono el botón de enviar, se obtendrá el valor de id y quiero crear un archivo, pero muestra un error para crear un archivo

código del archivo html: -

 <label for="">Make New File : </label> <input type="text" id="inputfilename"> <p id="output"></p> <br> <button id="submitbutton" onclick="submit()">Make File</button>

código para javascript y nodo js: -

 function submit() { const filename = document.getElementById("inputfilename").value; console.log(filename); //make file // const filepath = `C:\Users\asus\Desktop${filesystem}`; // console.log(filepath); const filesystem = require('fs'); filesystem.writeFileSync('xyz.txt','Hiii',(err)=>{ if(err) { // console.log('Error making file...'); document.getElementById("output").innerHTML = "Error making file..."; return; } else { // console.log("File is created..."); document.getElementById("output").innerHTML = "File is created!"; } }); }

pero muestra un error como este en la consola: -

 Uncaught ReferenceError: require is not defined at submit (indexjs.js:10:24) at HTMLButtonElement.onclick ((index):15:50)
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Cuando usa javascript desde el lado del cliente, puede usar Blob y el módulo de ahorro de archivos para crear un archivo.

 import { saveAs } from 'file-saver'; var blob = new Blob(["Welcome to Websparrow.org."], { type: "text/plain;charset=utf-8" }); saveAs(blob, "static.txt");

about 4 years ago · Juan Pablo Isaza Report

0

Prueba esto

html

 <form onSubmit={handleSubmit}> <label for="inputfilename">File Name: </label> <input type="text" name="inputfilename"> <p id="content">some content</p> <button type="submit" id="submitbutton">Make File</button> </form>

js

 const fs = require('fs'); async function handleSubmit() { const filename = document.getElementById("inputfilename").value; const content = document.getElementById("content").innerHTML; try { await fs.writeFileSync(`/Users/joe/${filename}`, content); } catch (err) { console.log(err); } }

Puedes mirar esto Escribiendo archivos con Node.js

about 4 years ago · Juan Pablo Isaza Report

0

js

 const filesystem = require('fs'); filesystem.writeFile(`demo.txt`, 'Hiii , Demo file content', (err) => { if (err) { console.log('Error making file...'); return; } else { console.log("File is created..."); } });
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!