CodeIgniter ajax upload image (ajax works, not CodeIgniter)

Linesofcode

I do know how to upload an image by ajax in a traditional way. However, with the library upload from CodeIgniter I can't figure it out.

But first, let me just tell you that I do not use a form at all.

if(!empty($_FILES)){

    $this->load->helper('global_helper');

    $config['upload_path']   = base_url() + "public/images/";
    $config['file_name']     = randomName();
    $config['allowed_types'] = "jpg|png|bmp";
    $config['max_size']   = '500';
    $config['overwrite']      = FALSE;

    $this->load->library('upload', $config);

    if(!$this->upload->do_upload()){
        $error = array('error' => $this->upload->display_errors());
        print_r($error);
    }
}

This does not work because I'm receiving the following error:

Array
(
    [error] => <p>You did not select a file to upload.</p>
)

But I actually did and it was actually uploaded to the server, because if I print out the $_FILES I receive:

Array
(
    [0] => Array
        (
            [name] => no_image.png
            [type] => image/png
            [tmp_name] => C:\xampp\tmp\php1715.tmp
            [error] => 0
            [size] => 6361
        )

)

Might be the problem of the input being in an Array(of Array[0])?

The javascript code is the following:

event.stopPropagation();
event.preventDefault();
var data = new FormData();

if(picture != undefined){ 
    $.each(picture, function (key, value) {
        data.append(key, value);
    });
}

$.ajax({
    url: 'ajax_add',
    type: 'POST',
    data: data,
    cache: false,
    processData: false,  
    contentType: false, 
    success: function(aux){
        console.log(aux);
    }
});

I do not want to use the traditional way, but if I have too..

Thanks.

Adrian Tombu

Not sure if it can work, because the file uploading class is designed to be used with a form, but you can try this

$this->upload->do_upload(0);

If it does what I think, it will select the first item of the $_FILES array. By default, it tries to find $_FILES['userfile'], who doesn't exists here (which is why you have the error).

Give it a try, and we'll see if it works :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can i upload image with ajax in codeigniter?

From Dev

Upload form with image in codeigniter using ajax

From Dev

codeigniter dropzone ajax upload with csrf

From Dev

codeigniter jquery ajax file upload

From Dev

codeigniter form upload with images using ajax

From Dev

CodeIgniter - Ajax file upload - not uploading the file

From Dev

CodeIgniter, Jquery Ajax, Form Submission and File Upload

From Dev

Upload files Using AJAX & CodeIgniter 3.0.1

From Dev

Upload files with Ajax in CodeIgniter with Jquery not working

From Dev

unable to upload file in codeigniter using AJAX

From Dev

how to upload image with ajax and codeigniter where my CI is IN var/www/html

From Dev

Ajax upload in CodeIgniter returns "you did not select a file to upload"

From Dev

Codeigniter 3 - Ajax File Upload (You did not select a file to upload)

From Dev

image upload in codeigniter

From Dev

Upload Image with FTP in Codeigniter

From Dev

codeigniter form with image upload

From Dev

Image upload is not working in codeigniter

From Dev

image upload in codeigniter

From Dev

Image upload issue in CodeIgniter

From Dev

Codeigniter profile image upload

From Dev

Image upload and view with codeigniter

From Dev

CodeIgniter reply using ajax only works on first data

From Dev

Send array with ajax in codeigniter

From Dev

Codeigniter displaying ajax request

From Dev

CodeIgniter Ajax is not working

From Dev

Codeigniter ajax autocomplite not working

From Dev

CodeIgniter Controller - JSON - AJAX

From Dev

JSON not working with CodeIgniter and AJAX

From Dev

search database with ajax and codeIgniter