0

I am writing a jQuery/AJAX script that uploads an image. The ajax goes through because my upload script returns a message. The issue is that the upload script does not find the selected file. I use codeigniter to execute the file upload and then output xml. I get the message 'You did not select a file to upload'

Here's my jQuery:

$('#submit-thumb').on('click', function(e) {
    console.log('submit detected');
    $.ajax({
        type: "POST",
        data: $('#thumbnail').serializeArray(),
        dataType: "xml",
        url: "http://basecommand.com/index.php/ajax/content/thumb/3yIfPuWkDwRCKosq",
        cache: false,
        success: function(xml) {
            $(xml).find('success').each(function() {
                $('#thumb-message').html('<p style="color: green;">'+$(this).find('message').text()+'</p>');
                $('#thumb-img').prop('src', $(this).find('filename').text());
            });
            $(xml).find('error').each(function() {
                $('#thumb-message').html('<p style="color: red;">'+$(this).find('message').text()+'</p>');
            });
        },
        error: function() {
            $('#thumb-message').html('<p style="color: red;">ajax failed to execute...</p>');
        }
    });

    e.preventDefault(); 
});

This is the AJAX file:

function thumb($draft_code) {

    $config = array(
        'allowed_types' => 'gif|jpg|png',
        'max_size' => '100',
        'max_width' => '192',
        'max_height' => '108',
        'encrypt_name' => TRUE
    );

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

    if($this->upload->do_upload('thumbnail')) {

        $this->attachment->create($this->upload->upload_data());

        if($this->attachment->error == NULL) {

            $this->article->draft_thumb($draft_code, $this->attachment->info['id']);

            if($this->article->error == NULL) {
                header('Content-Type: text/xml');
                $this->output->set_output(
                    '<success>
                        <message>Your thumbnail has been changed.</message>
                        <filename>'.$this->attachment->info['file_name'].'</filename>
                    </success>'
                );
            } else {
                header('Content-Type: text/xml');
                $this->output->set_output(
                    '<error>
                        <message>'.$this->article->error.'</message>
                    </error>'
                );
            }
        } else {
            header('Content-Type: text/xml');
            $this->output->set_output(
                '<error>
                    <message>'.$this->attachment->error.'</message>
                </error>'
            );
        }
    } else {    
        header('Content-Type: text/xml');
        $this->output->set_output(
            '<error>
                <message>'.$this->upload->display_errors().'</message>
            </error>'
        );
    }
}

This is the HTML:

<a href="#change-thumb" rel="toggleElement">Change Thumbnail</a>                        

<div style="display: none;" id="change-thumb">
    <input type="file" name="thumbnail" id="thumbnail">
    <a href="" id="submit-thumb">Submit</a>
    <div id="thumb-message"></div>
</div>
C1pher
  • 1,933
  • 6
  • 33
  • 52
ShoeLace1291
  • 4,551
  • 12
  • 45
  • 81
  • possible duplicate of [How can I upload files asynchronously with jQuery?](http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery) – Spokey Oct 18 '14 at 23:45
  • SO how is any of that any different from what I am doing? – ShoeLace1291 Oct 18 '14 at 23:55
  • 1
    You need to use [`FormData()`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) and set `processData` and `contentType` to `false` just like in the answer given you the linked question `.serializeArray()` will only get the value of the input, and the value of a file input is the name of the file and not the file itself. – Spokey Oct 19 '14 at 16:34

0 Answers0