Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to validate the size and type of input=file in html5

I was reading this http://dev.w3.org/html5/markup/input.file.html, but I only found the "accept" attribute.

I tried this

<input type="file" name="imagefilename" accept="image/x-png, image/gif, image/jpeg" /> 

Is it possible client-side validation of file size?

I found this technique for IE

<html> <head> <script> function getSize() {     var myFSO = new ActiveXObject("Scripting.FileSystemObject");     var filepath = document.upload.file.value;     var thefile = myFSO.getFile(filepath);     var size = thefile.size;     alert(size + " bytes"); } </script> </head> <body> <form name="upload"> <input type="file" name="file"> <input type="button" value="Size?" onClick="getSize();"> </form> </body> </html> 

Is it possible to do the same using html5 filesystem api ?

UPDATE

I could do this (demo):

<!doctype html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script> </head> <body>     <form >         <input type=file id=f max-size=32154 >         <input type=submit>     </form> <script> $(function(){     $('form').submit(function(){         var isOk = true;         $('input[type=file][max-size]').each(function(){             if(typeof this.files[0] !== 'undefined'){                 var maxSize = parseInt($(this).attr('max-size'),10),                 size = this.files[0].fileSize;                 isOk = maxSize > size;                 return isOk;             }         });         return isOk;     }); }); </script> </body> 

It is working fine, but I think It will be better to a native html5 attr to validate

like image 663
Martin Borthiry Avatar asked Nov 21 '11 12:11

Martin Borthiry


People also ask

How do I know the size of my HTML5 file?

Determining the size of the file using jQuery and HTML5Inside the click event handler, first a check is performed to verify whether the browser supports HTML5 File API. If the browser supports HTML5 File API then the size of the file is determined and the displayed.

How do I set max file size in HTML?

&lt;input type='file' name='txtUploadFileName' /&gt; Then the value in the hidden input with name 'MAX_FILE_SIZE' is submitted with the when the submit button is clicked. The server then automatically checks if the size of the file exceeds the value of MAX_FILE_SIZE.

How do I hide the selected text file in HTML?

You can give the input element a font opacity of 0. This will hide the text field without hiding the 'Choose Files' button.


2 Answers

    <form  class="upload-form">         <input class="upload-file" data-max-size="2048" type="file" >         <input type=submit>     </form>     <script> $(function(){     var fileInput = $('.upload-file');     var maxSize = fileInput.data('max-size');     $('.upload-form').submit(function(e){         if(fileInput.get(0).files.length){             var fileSize = fileInput.get(0).files[0].size; // in bytes             if(fileSize>maxSize){                 alert('file size is more then' + maxSize + ' bytes');                 return false;             }else{                 alert('file size is correct- '+fileSize+' bytes');             }         }else{             alert('choose file, please');             return false;         }      }); });     </script> 

http://jsfiddle.net/9bhcB/2/

like image 184
Serg Hospodarets Avatar answered Sep 29 '22 04:09

Serg Hospodarets


I could do this (demo):

<!doctype html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script> </head> <body>     <form >         <input type="file" id="f" data-max-size="32154" />         <input type="submit" />     </form> <script> $(function(){     $('form').submit(function(){         var isOk = true;         $('input[type=file][data-max-size]').each(function(){             if(typeof this.files[0] !== 'undefined'){                 var maxSize = parseInt($(this).attr('max-size'),10),                 size = this.files[0].size;                 isOk = maxSize > size;                 return isOk;             }         });         return isOk;     }); }); </script> </body> </html> 
like image 26
Martin Borthiry Avatar answered Sep 29 '22 04:09

Martin Borthiry