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

1.7K
Views
¿Cómo encontrar el tamaño del archivo en Node.js?

Estoy usando multer para cargar mis imágenes y documentos, pero esta vez quiero restringir la carga si el tamaño de la imagen es> 2mb. ¿Cómo puedo encontrar el tamaño del archivo del documento? Hasta ahora probé como se indica a continuación, pero no funcionó.

 var storage = multer.diskStorage({ destination: function (req, file, callback) { callback(null, common.upload.student); }, filename: function (req, file, callback) { console.log(file.size+'!!!!!!!!!!!!!!')======>'Undefined' var ext = ''; var name = ''; if (file.originalname) { var p = file.originalname.lastIndexOf('.'); ext = file.originalname.substring(p + 1); var firstName = file.originalname.substring(0, p + 1); name = Date.now() + '_' + firstName; name += ext; } var filename = file.originalname; uploadImage.push({ 'name': name }); callback(null, name); } });

¿Alguien puede ayudarme por favor?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Para obtener el tamaño de un archivo en megabytes:

 var fs = require("fs"); // Load the filesystem module var stats = fs.statSync("myfile.txt") var fileSizeInBytes = stats.size; // Convert the file size to megabytes (optional) var fileSizeInMegabytes = fileSizeInBytes / (1024*1024);

o en bytes:

 function getFilesizeInBytes(filename) { var stats = fs.statSync(filename); var fileSizeInBytes = stats.size; return fileSizeInBytes; }
over 4 years ago · Santiago Trujillo Report

0

Si usa ES6 y deconstruye, encontrar el tamaño de un archivo en bytes solo toma 2 líneas (¡una si el módulo fs ya está declarado!):

 const fs = require('fs'); const {size} = fs.statSync('path/to/file');

Tenga en cuenta que esto fallará si la variable de tamaño ya se declaró. Esto se puede evitar cambiando el nombre de la variable mientras se deconstruye usando dos puntos:

 const fs = require('fs'); const {size: file1Size} = fs.statSync('path/to/file1'); const {size: file2Size} = fs.statSync('path/to/file2');
over 4 years ago · Santiago Trujillo Report

0

Además, puede usar el paquete de tamaño de archivo de NPM: https://www.npmjs.com/package/filesize

Este paquete hace que las cosas sean un poco más configurables.

 var fs = require("fs"); //Load the filesystem module var filesize = require("filesize"); var stats = fs.statSync("myfile.txt") var fileSizeInMb = filesize(stats.size, {round: 0});

Para más ejemplos:
https://www.npmjs.com/package/filesize#examples

over 4 years ago · Santiago Trujillo 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!