为什么我的jQuery没有处理我的文件输入?

时间:2014-10-18 23:17:59

标签: jquery ajax codeigniter

我正在编写一个上传图片的jQuery / AJAX脚本。 ajax通过,因为我的上传脚本返回一条消息。问题是上载脚本找不到所选文件。我使用codeigniter执行文件上传,然后输出xml。我收到消息“您没有选择要上传的文件”

这是我的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(); 
});

这是AJAX文件:

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>'
        );
    }
}

这是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>

0 个答案:

没有答案