Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing the error message to jQuery File Upload

I've integrated JQuery File Upload into a Java Spring application. The returned JSON is generated from an array of Picture, where Picture contains "name", "size", "url", "thumbnail_url", "delete_url" and "delete_type".

Sometimes I decide to not accept the upload on the server, to reject it, because of missing pre-conditions, so I would like to inform inform the user about it, by returning to the client an error message.

I know it's possible to return an error message and an error code to the File Upload plugin, but I can't find it in the documentation. I suppose that I've to add two fields similar to "error_message" and "error_code".

Where can I find this documentation or what are the field names that I should return.

like image 835
stivlo Avatar asked Nov 11 '11 17:11

stivlo


3 Answers

You can use data.jqXHR.responseText

Like this:

fail: function(e, data){
    console.log(data.jqXHR.responseText);
},
like image 127
Lazaro Fernandes Lima Suleiman Avatar answered Nov 09 '22 11:11

Lazaro Fernandes Lima Suleiman


By looking at the source code (the ultimate documentation), I found out that File Upload checks an "error" field and displays that error. I tried and it works, my error gets displayed.

The JSON response is an array of Objects, one per file uploaded. In case of error, don't fill URLs and size. The important properties are the error and name and size, that will be displayed to the user.

[ 
    {
        "error": "Image must be in JPG format",
        "url": "", 
        "thumbnail_url": "", 
        "delete_url": "", 
        "delete_type": "DELETE", 
        "name": "broken_image.jpg", 
        "size": 78191
     }
]
like image 22
stivlo Avatar answered Nov 09 '22 10:11

stivlo


done: function (e, data){
    var files = data.jqXHR.responseJSON.files;
    console.log(files);
}),

Each "files" object can have an error var you can work with, like user stivlo said.

like image 1
Allfarid Morales García Avatar answered Nov 09 '22 11:11

Allfarid Morales García