doudou348131346 2012-05-22 20:26
浏览 45
已采纳

Codeigniter Ajax上传图片

I'm currently developping some forms for my school project with CodeIgniter.

The idea is that I have a form with image upload. I'm trying to do it dynamically with Ajax but it seems not working at all. I tried the non-dynamic version with php and It works perfectly, my images are in my folder and I have no problem with it.

I tried like 5 or 6 plug-ins with no results, it is certainly my fault but I don't know where I did a mistakes.

<---Controller--->

if($result = $this->images_model->add_bdd())
{   
    $data['uploaded'] = $result;
    $data['message_upload'] = 'Image uploader avec succès.';                            
    $this->template->set_title('Upload successful');
    $this->template->view('add_places',$data);
}
else
{   
    $this->template->set_title('Upload failed');
    $this->template->view('add_places');
}

<--Model-->

function add_bdd()
{
    $config = array(
                'allowed_types' => 'jpg|jpeg|tiff',
                'upload_path' => $this->gallery_path,
                'max_size' => 2000,
                'remove_spaces' => TRUE,
                'overwrite' => FALSE
            );

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

        $data_img = $this->upload->data();
        $exif = $this->exif_extractor->coords($data_img['full_path']);
        $data = array(
                'titre' => 'titlecontent',
                'url' => base_url('images/'.$data_img['file_name']),
                'url_min' => base_url('images/'.$data_img['raw_name'].'_min'.$data_img['file_ext']),
                'alt' => 'cover_contentName',
                'id_users' => $this->session->userdata('id'),
                'date_upload' => date('Y-m-d H:m'),
                'date_modified' => date('Y-m-d H:m'),
                'lat' => $exif[0],
                'long' => $exif[1],
                );
        $this->db->insert('pictures',$data);
        return $exif;
    }
    else
    {
        return false;
    }
}

<--View-->

<form action="http://localhost:8888/project/images/add" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<input type="file" name="userfile" value="Image de couverture"  />
<button name="upload" type="button" id="upload">Upload</button>
<input type="submit" name="submit" value="Publish Place"  />
</form>

Can anyone give me a jQuery plugin to upload images dynamically and send back to the script my images with the path and others data that I want to return ?

I can't paste like all the code I made for jQuery, but I really need help about it. It's been 2 days that I'm on it!

Thanks for your help.

  • 写回答

3条回答 默认 最新

  • duancan8382 2012-05-27 21:50
    关注

    Thanks for you help, I change my dynamic about the script so now, it works perfectly with what I have to do.

    There is the script for my upload system :

    <-- VIEW (upload_img)-->

    <div id="container">
    <div id="filelist"></div>
    <a href="http://localhost:8888/myproject/#" id="userfile" name="userfile">[Select files]      </a>    
    <a href="http://localhost:8888/myproject/images/index#" id="uploadfiles">[Upload files]</a
    </div>
    

    <-- CONTROLLER -->

    function index(){
        if($this->_access())
        {
            $this->template->add_include('js/jquery.js')
                           ->add_include('js/browserplus.js')
                           ->add_include('js/plugins/plupload/plupload.full.js')
                           ->add_include('js/imgfunc.js');
            $this->template->view('upload_img');
        }
        else
        {
            redirect(site_url());
        }
    }
    
    function upload_image_spot()
    {
        if($query = $this->images_model->upload_image_spot())
        {
            echo json_encode($query);
        }
        else
        {
            echo $this->upload->display_errors('', '');
        }
    }
    

    <-- MODEL -->

       function upload_image_spot()
    {
        $config['upload_path'] = realpath(APPPATH. '../images/spots');
        $config['allowed_types'] = 'jpg|jpeg|tiff|png';
        $config['max_size'] = 3062;
        $config['encrypt_name'] = TRUE;
        $this->load->library('upload', $config);
        if($this->upload->do_upload('file')) 
        // file means the file send to the website for upload, this is the name of field for Plupload script
        {
        $data_img = $this->upload->data();
        $copies = array(
                array('dir' => 'images/spots/large/', 'x' => 1000, 'y' => 600),
                array('dir' => 'images/spots/thumb/', 'x' => 100, 'y' => 60)
        );
    
        $this->copies($data_img,$copies);
    
        return 'whatever'; // Not real data, I don't wanna post them here
        }
    }
    

    <-- JS-SCRIPTS -->

    First of all include :

    -jQuery

    -browserPlus

    -Plupload

    (Plupload Script)

    Now add this script in an empty file:

    var uploader = new plupload.Uploader({
        runtimes : 'gears,html5,flash,silverlight,browserplus',
        browse_button : 'userfile',
        container : 'container',
        max_file_size : '3mb',
        url : 'yourUploadFunctionCI',
        flash_swf_url : 'plugins/plupload/js/plupload.flash.swf',
        silverlight_xap_url : 'plugins/plupload/js/plupload.silverlight.xap',
        filters : [
            {title : "Image files", extensions : "jpg,jpeg,JPG,JPEG,tiff,png"},
        ]
    });
    
    uploader.bind('Init', function(up, params) {
    });
    
    $('#uploadfiles').click(function(e) {
        uploader.start();
        e.preventDefault();
    });
    
    uploader.init();
    
    uploader.bind('FilesAdded', function(up, files) {
        $.each(files, function(i, file) {
            $('#filelist').html("");
            $('#filelist').append(
                '<div id="' + file.id + '">' +
                file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' +
            '</div>');
        });
    
        up.refresh();
    });
    
    uploader.bind('UploadProgress', function(up, file) {
        $('#' + file.id + " b").html(file.percent + "%");
    });
    
    uploader.bind('Error', function(up, err, msg,file) {
        $('#filelist').append("<div>Error: " + err.code +
            ", Message: " + err.message +
            (err.file ? ", File: " + err.file.name : "") +
            "</div>"
        );
        console.log(msg,up,err);
        up.refresh();
    });
    
    uploader.bind('FileUploaded', function(up, file,response) {
        $('#' + file.id + " b").html("100%");
        var data = jQuery.parseJSON(msg.response);
        console.log(data);
    });
    

    Do your own customization and that's it, no need extra script like you can see on website like copy/paste all script from a php file to a controller, just add 'file' inside do_upload and everything's gonna work fine !

    Have a nice day guys, hope it's help.

    Simon

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图