I need to upload a file in my server on file input change. I used the library for file input from Codeigniter.
index
<form class="image" method="POST">
<input id="image-value" data-id="{{id}}" name="{{picture}}" class="image-box-{{id}}" data-type="{{type}}" type="file" value="{{picture}}" />
</form>
here is the view where I made a form that accepts an image
script
$('[id="image-value"]').change(function(e) {
var file_data = $(this).prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
$.ajax({ // 02102016 AA: picture_caption
url: './upload_picture/',
dataType: 'json',
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function(data){
alert(data);
},
error: function(jqXHR, textStatus, errorThrown) {
console.error("The following error occured: " + textStatus, errorThrown);
}
});
when triggered, the script gets the file from the index and send it to my controller
controller
function __construct() {
parent::__construct();
$config['upload_path'] = './data/picture/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
$this->upload->initialize($config);
}
function upload_picture() {
// $data = Array();
log_message("debug", "do_upload: ".$this->upload->do_upload());
if ($this->upload->do_upload()) {
log_message("debug", "data: ".$this->upload->data());
}
the problem (i think) is in how i passed the data from my controller
as the value of do_upload is still false even the the data has been
passed from the script
I found a great solution!
turns out it just needed
"file"as do_upload() parameter. thanks for this great article! saved my life!
<form class="image" method="POST" enctype="multipart/form-data">but i still get false