How to reset a file type input?

1

I have a problem with the following code, what I want to do is that if the image is larger than 3.67 MB, I warn the user that it is very large, and that he does not select the image, since when he gives the alert the user the image is still selected.

<!DOCTYPE html>
<html lang="es-ar">
<head>
    <meta charset="utf-8" />
    <title>Tamaño archivo</title>
</head>
<body>
    <form action="#">
         <input type="file" id="archivos" accept="image/*" name="archivos[]"/>
    </form>
    <script>
        function archivoSeleccionado(evt) {
            if(window.File && window.FileReader && window.FileList && window.Blob){
                 var image = this.files[0].size;
                 if(image >= 3856819){
                       alert("La imagen es muy grande, El tamaño maximo es de 3.67 MB");
                 }else{
                       alert("La imagen tiene el tamaño adecuado");
                  }
            }

       }
       document.getElementById('archivos').addEventListener('change',    archivoSeleccionado, false);
    </script>
</body>
</html>
    
asked by OrlandoVC 04.03.2017 в 18:26
source

3 answers

0

The classic solution is to assign an empty string to the element:

if(image >= 3856819) {
  alert("La imagen es muy grande, El tamaño maximo es de 3.67 MB");
  this.value = '';
}

However, keep in mind that if you support IE

answered by 04.03.2017 / 20:20
source
1

         File size                                  function fileSelected (evt) {             if (window.File & & window.FileReader & & window.FileList & & window.Blob) {                  var image = this.files [0] .size;

             if(image >= 3856819){
                   alert("La imagen es muy grande, El tama�o maximo es de 3.67 MB");
             }else{
                   alert("La imagen tiene el tama�o adecuado");
              }
        }

   }
   document.getElementById('archivos').addEventListener('change',    archivoSeleccionado, false);
    function limpiar(){
        input=document.getElementById("archivos");
        input.value = ''

input.type = '' input.type = 'file'

    }
</script>
<input type="button" onClick="limpiar()" value="Limpiar">

    
answered by 04.03.2017 в 19:38
0

This should work for you if(image >= 3856819) { alert("La imagen es muy grande, El tamaño maximo es de 3.67 MB"); var inputImage = document.getElementById("archivos"); inputImage.value = ''; }

    
answered by 04.03.2017 в 21:11