I'm using Blueimp jQuery file upload plugin for upload files.
I had no problem in uploading but the option maxFileSize
and acceptFileTypes
do not work.
This is my code:
$(document).ready(function () {
'use strict';
$('#fileupload').fileupload({
dataType: 'json',
autoUpload: false,
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
maxFileSize: 5000000,
done: function (e, data) {
$.each(data.result.files, function (index, file) {
$('<p style="color: green;">' + file.name + '<i class="elusive-ok" style="padding-left:10px;"/> - Type: ' + file.type + ' - Size: ' + file.size + ' byte</p>')
.appendTo('#div_files');
});
},
fail: function (e, data) {
$.each(data.messages, function (index, error) {
$('<p style="color: red;">Upload file error: ' + error + '<i class="elusive-remove" style="padding-left:10px;"/></p>')
.appendTo('#div_files');
});
},
progressall: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('#progress .bar').css('width', progress + '%');
}
});
});
Had the same problem, and the blueimp guy says "maxFileSize and acceptFileTypes are only supported by the UI version" and has provided a (broken) link to incorporate the _validate and _hasError methods.
So without knowing how to incorporate those methods without messing up the script I wrote this little function. It seems to work for me.
Just add this
at the start of the .fileupload options as shown in your code here
You'll notice I added a filesize function in there as well because that will also only work in the UI version.
Updated to get past issue suggested by @lopsided: Added
data.originalFiles[0]['type'].length
anddata.originalFiles[0]['size'].length
in the queries to make sure they exist and are not empty first before testing for errors. If they don't exist, no error will be shown and it will only rely on your server side error testing.open the file named "jquery.fileupload-ui.js", you will see the code like this:
just add one line code --- the new attribute "acceptFileTypes",like this:
now you'll see everything is allright!~ you just take the attribute with a wrong place.
Checked/Valid example for:
$.grep()
to remove files from array with errorsimage
andaudio
formatnew RegExp()
Notice:
acceptFileTypes.test()
- check mime types, for adio file like.mp3
it will beaudio/mpeg
- not only extenstion. For all blueimp options: https://github.com/blueimp/jQuery-File-Upload/wiki/OptionsYou should include jquery.fileupload-process.js and jquery.fileupload-validate.js to make it work.
Then...
processfail callback is launched after a validation fail.
As suggested in an earlier answer, we need to include two additional files -
jquery.fileupload-process.js
and thenjquery.fileupload-validate.js
However as I need to perform some additional ajax calls while adding a file, I am subscribing to thefileuploadadd
event to perform those calls. Regarding such a usage the author of this plugin suggested the followingUsing the combination of the two suggested options, the following code works perfectly for me
If you've got all the plugin JS's imported and in the correct order, but you're still having issues, it seems that specifying your own "add" handler nerfs the one from the *-validate.js plugin, which normally would fire off all the validation by calling data.process(). So to fix it just do something like this in your "add" event handler: