简体   繁体   中英

jQuery-File-Upload (blueimp) - how do I access the error value both in JS end and PHP

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM