Here is my angularjs code: In Controller:
var formdata = new FormData();
$scope.getTheFiles = function ($files) {
console.log($files);
angular.forEach($files, function (value, key) {
formdata.append(key, value);
});
};
// NOW UPLOAD THE FILES.
$scope.uploadFiles = function () {
var request = {
method: 'POST',
url: 'server/appointments/uploadedFiles',
data: formdata,
headers: {
'X-API-KEY': $rootScope.currentUser.key,
'Content-Type': undefined
}
};
// SEND THE FILES.
$http(request)
.success(function (d) {
console.log(d);
})
.error(function (e) {
console.log(e);
});
}
}
And In Directive:
.directive('ngFiles', ['$parse', function ($parse) {
function fn_link(scope, element, attrs) {
var onChange = $parse(attrs.ngFiles);
element.on('change', function (event) {
onChange(scope, { $files: event.target.files });
});
};
return {
link: fn_link
}
} ]);
For API i use codeigniter:
function uploadedFiles_POST()
{
function do_upload()
{
if ($this->input->post('Upload')) {
$config['upload_path'] = './Appointments/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
} else {
$data = $this->upload->data();
$file = array(
'img_name' => $data['raw_name'],
'ext' => $data['file_ext'],
'upload_date' => time()
);
$this->appointments_model->add_image($file);
$data = array('upload_data' => $this->upload->data());
}
} else {
redirect(site_url('upload'));
}
}
}
And in codeigniter model:
function add_image($data)
{
$this->db->where('id', '395');
return $this->db->update('user_appoint', $data);
}
My problem is browser detect file name in developer tool but file cant upload to database. it show 500 status.