Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
13
rated 0 times [  19] [ 6]  / answers: 1 / hits: 42799  / 9 Years ago, tue, april 14, 2015, 12:00:00

I'm using Krajee the Bootstrap File Input plugin to perform an upload via AJAX call.



Here is the link to the Krajee plugin AJAX section: Krajee plugin AJAX



The JS and PHP (codeigniter) codes I'm using are as following:



JS:



<script>        
$(#file-upload).fileinput({
'allowedFileExtensions' : ['csv'],
'maxFileSize': 5120,
'maxFileCount': 1,
'uploadUrl': 'dashboard/uploader',
'elErrorContainer': '#errorBlock',
'uploadAsync': true,
'msgInvalidFileExtension': 'Invalid extension for file {name}. Only {extensions} files are supported.',
'uploadExtraData': {csrf_token_name: $(input[name=csrf_token_name]).val()}
});
</script>


PHP:



public function uploader(){
$config['upload_path'] = './csv_uploads/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '5120';

$this->upload->initialize($config);
if (!$this->upload->do_upload(file-upload)){
$data['error'] = 'The following error occured : '.$this->upload->display_errors().'Click on Remove and try again!';
echo json_encode($data);
} else {
echo json_encode(success);
}
}


Right now I get a response from PHP whatever it is an error or a success as JSON, I have went through the plugin documentation and I still can't find how to catch the AJAX response and act according to that response as we do in jQuery with the ajax success function :



success: function (response) {
//Deal with the server side response data.
},


How can I do this?


More From » php

 Answers
6

You can check out a demo here live demo



Remember to set uploadAsync false if you want the success event fire



Example code:



JS



$(#input-id).fileinput({
showRemove:false,
showPreview: false,
uploadUrl: ../xxxx/xxxx/XXXXXX.php, // server upload action
uploadAsync: false,
uploadExtraData: function() {
return {
bdInteli: xxxx
};
}
});

// CATCH RESPONSE
$('#input-id').on('filebatchuploaderror', function(event, data, previewId, index) {
var form = data.form, files = data.files, extra = data.extra,
response = data.response, reader = data.reader;
});

$('#input-id').on('filebatchuploadsuccess', function(event, data, previewId, index) {
var form = data.form, files = data.files, extra = data.extra,
response = data.response, reader = data.reader;
alert (extra.bdInteli + + response.uploaded);
});


PHP



$nombre = $_FILES[ficheroExcel][name];
$bdInteli = $_POST['bdInteli'];
if (move_uploaded_file($_FILES[ficheroExcel][tmp_name], $nombre) ){
$output = array('uploaded' => 'OK' );
} else {
$output = array('uploaded' => 'ERROR' );
}
echo json_encode($output);

[#67082] Saturday, April 11, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tayla

Total Points: 681
Total Questions: 102
Total Answers: 108

Location: Marshall Islands
Member since Tue, Sep 21, 2021
3 Years ago
tayla questions
Fri, Mar 5, 21, 00:00, 3 Years ago
Wed, Oct 28, 20, 00:00, 4 Years ago
Thu, Apr 9, 20, 00:00, 4 Years ago
;