0

How do I test if the file uploaded has returned an error using blueimps file uploader.

The uploader only allows a single file upload (must be an image).

I have the following js code

<script>
$(function () {
    $('#fileupload').fileupload({
    dataType: 'json',
    singleFileUploads: true ,
    submit: function (e, data) {
       $('#upload_overlay').fadeIn(300);
    },
    done: function (e, data) {



      if(typeof data.files.error == "undefined"){
        $('#err_succ_msg').html('Photo successfully updated.').css('background-color','#B1DD8B').show(1);
        $('#upload_overlay').fadeOut(300);
      }
      else{

        $('#err_succ_msg').html('Photo update failed. Please try again.').css('background-color','#F76151').show(1);
        $('#upload_overlay').fadeOut(300);
      }

    },

    fail: function (e, data) {
      $('#err_succ_msg').html('Photo update failed. Please try again.').css('background-color','#F76151').show(1);
      $('#upload_overlay').fadeOut(300);
    }
    });
});
</script>

In the "done" function I'm trying to find out if there is a error so I can show the correct message to the user.

I've tried "alerting" out data.result[0].error and data.files[data.index].error but it either comes out blank or has a "data.result[0]" is undefined.

Mostly from this question here: blueImp/jquery file upload - How do I get the error message if the file type was not accepted?

also on the PHP side of things how do I check if there is an error. For example the PHP index file has this:

require('UploadHandler.php');
$upload_handler = new UploadHandler();

What do I need to figure out if there is an error so I do some processing on the server side?

2
  • apologies for the poor formatting - for the life of me I cant figure out how I make the code look pretty with the syntax heighlighting. Commented May 19, 2014 at 14:59
  • @chresse - thanks for the re-formatting! Commented May 19, 2014 at 15:06

1 Answer 1

4

Just to follow up on this. This is how I accessed the object data on the JS side:

data['jqXHR']['responseJSON']['files'][0]['error']

so getting the name of the file for example would be

data['jqXHR']['responseJSON']['files'][0]['name']

On the PHP side in the end I just put my code in the function "body" in the file uploadHandler.php

I had to convert the JSON formatted string into an array to access the data

protected function body($str) { $new_array = json_decode($str, true); ....

So I could access this like this

$new_array['files'][0]['name']

I hope that helps someone.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks @KB but responseJSON inside the jqXHR only returns the JSON array returned by the server right ?? So, if my backend code don't return filenames on success does it mean that I will be unable to display the files uploaded ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.