Search code examples
jqueryjquery-file-uploadblueimp

Original filename in case of error in jQuery File Upload


Using jQuery File Upload, how is it possible to retrieve the original filename when an upload fails, in the fileuploadfail event handler?

$('#fileupload').fileupload({
   ...
})
.on('fileuploadfail', function (evt, data) {
   // retrieve original filename
});

I did not find the answer in the documentation, and could not find anything in the event object or in the data parameter.


Solution

  • I've checked the source code and found out that the function parameters doesn't pass the filename, so you can store the filename in a variable (for example) when you add the file and use it when there is an error:

    var fileName;
    $('#fileupload').fileupload({
        ....
    }).on('fileuploadadd', function (e, data) {
        fileName = data.files[0].name;
    }).on('fileuploadfail', function (evt, data) {
        alert(fileName);
    });
    

    Edit: As written in the comments, apparently there is an option to get the filename in the fail function by referring to data.files[0].name - Thanks @FlorentGeorges