Skip to content

Instantly share code, notes, and snippets.

@jacksonfdam
Created March 6, 2013 19:36
Show Gist options
  • Save jacksonfdam/5102342 to your computer and use it in GitHub Desktop.
Save jacksonfdam/5102342 to your computer and use it in GitHub Desktop.
Restringir o tipo de arquivos no upload, pelo front-end.
<input type="file" accept="image/jpg,image/jpeg,image/png,image/gif,image/bmp,image/tiff" />
http://stackoverflow.com/questions/651700/how-to-have-jquery-restrict-file-types-on-upload
Ou
$('INPUT[type="file"]').change(function () {
var ext = this.value.match(/\.(.+)$/)[1];
switch (ext) {
case 'jpg':
case 'jpeg':
case 'png':
case 'gif':
$('#uploadButton').attr('disabled', false);
break;
default:
alert('This is not an allowed file type.');
this.value = '';
}
});
OU
var ext = $('#my_file_field').val().split('.').pop().toLowerCase();
if($.inArray(ext, ['gif','png','jpg','jpeg']) == -1) {
alert('invalid extension!');
}
OU
<input type="file" id="userfile" accept="image/*|video/*" required />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment