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

112
Views
validación de javascript para la carga de archivos múltiples antes de cargarlos en html

Quiero validar que los múltiples archivos que subo no superen los 2MG, así que solo necesito un código javascript para manejar esa situación.

<input type="file" id="file" multiple>

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

0

la publicación que te recomendé tenía la respuesta, la programación no es solo ctrl+c y ctrl+v

 document.getElementById("fileinput").addEventListener("change", function showFileSize() { // (Can't use `typeof FileReader === "function"` because apparently it // comes back as "object" on some browsers. So just see if it's there // at all.) if (!window.FileReader) { // This is VERY unlikely, browser support is near-universal console.log("The file API isn't supported on this browser yet."); return; } const maxAllowedSize = 2 * 1024 * 1024; var input = document.getElementById('fileinput'); if (!input.files) { // This is VERY unlikely, browser support is near-universal console.error("This browser doesn't seem to support the `files` property of file inputs."); } else if (!input.files[0]) { addPara("Please select a file before clicking 'Load'"); } else { Array.from(input.files).forEach(file => { if(file.size > maxAllowedSize) { addPara(file.name + ": error"); }else { addPara(file.name + ": success"); } }); } }); function addPara(text) { var p = document.createElement("p"); p.textContent = text; document.body.appendChild(p); }
 body { font-family: sans-serif; }
 <form action='#' onsubmit="return false;"> <input type='file' id='fileinput' multiple="multiple"> </form>

about 4 years ago · Juan Pablo Isaza Report

0

Respuesta de: Esta solución

 const input = document.getElementById('input') const textDemo = document.getElementById('demo') input.addEventListener('change', (event) => { const target = event.target if (target.files && target.files[0]) { /*Maximum allowed size in bytes 5MB Example Change first operand(multiplier) for your needs*/ const maxAllowedSize = 5 * 1024 * 1024; textDemo.innerHTML = "Within 5MB limit" if (target.files[0].size > maxAllowedSize) { // Here you can ask your users to load correct file target.value = '' textDemo.innerHTML = "Exceeds 5MB limit" } } })
 <input type="file" id="input" /> <div id="demo"></div>

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!