check file existence on file add
Hi, I need to check is a file name is duplicated or not, and I would like to check this right on file add, before submit.
I´ve checked documentation and managed to get an alert on file add, following your documentation.
I´ve added this code to application.js and successfully got an alert :
$('#fileupload').bind('fileuploadadd', function (e, data) {
$.each(data.files, function (index, file) {
alert('Added file: ' + file.name);
//check name duplication here
});
})
My problem is that I need here to invoke some php function that will check if file already exists, and dont have a clue how to do it. I think this more a jquery problem than anything else, but somebody might know because I´m new to this.
Thanks !!
So I have find the solution myself. Here´s what I´ve done, it might help someone in application.js in example dir add this inside $(function () {
//callbacks
//https://github.com/blueimp/jQuery-File-Upload/wiki/Options
$('#fileupload').bind('fileuploadadd', function (e, data) {
$.each(data.files, function (index, file) {
//alert('Added file: ' + file.name);
var jqxhr = $.post("test.php",{ name: file.name}, function(resultado) {
//alert("sent " + resultado);
console.log(resultado);
//console.log("teste");
console.log(resultado.name_file_sent);
},"json")
.success(function() { alert("success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });
});
})
Then create a test.php file with this inside
"John","time"=>"2pm","name_file_sent"=>$file)); ?>Now, each time someone add a file, and before upload it will print in firebug console the name of the file.
Another way to do this on the client-end is to add an option:
currentFileNames: []
... and in _onAdd:
var that = this,
result = true,
options = $.extend({}, this.options, data),
uncheckedSet = data.files,
limit = options.limitMultiFileUploads,
i;
var fileSet = [];
var nameMatches = false;
$.each(uncheckedSet, function (index, file) {
$.each(options.currentFileNames, function (index2, existingFile) {
if (file.name === existingFile)
nameMatches = true;
});
if (nameMatches === false) {
fileSet.push(file);
options.currentFileNames.push(file.name);
}
});
if (fileSet.length <= 0)
return false;