实现jquery上传插件'uploadify'与codeigniter [英] Implementing jquery upload plugin 'uploadify' with codeigniter

查看:93
本文介绍了实现jquery上传插件'uploadify'与codeigniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在codeigniter中实现此功能? Uploadify(Jquery)



我的意思是什么是控制器?如何跟踪进度?如果用户没有安装flashplayer,会发生什么?



如何检查progess?有没有人尝试这个与codeigniter?

解决方案

我已经成功实现uploadify在codeigniter如下:



在标题视图中:

 < script type = text / javascriptsrc =<?php echo base_url()?> resources / js / jquery.js>< / script> 
< link rel =stylesheettype =text / cssmedia =allhref =<?php echo base_url()?> resources / uploadify / uploadify.css/>
< script type =text / javascriptsrc =<?php echo base_url()?> resources / uploadify / swfobject.js>< / script&
< script type =text / javascriptsrc =<?php echo base_url()?> resources / uploadify / jquery.uploadify.v2.1.0.min.js>< / script> ;

在页面查看

 < input id =fileInputname =fileInputtype =file/> 
< script type =text / javascript>
$(document).ready(function(){
$(#fileInput)。uploadify({
'uploader':'<?php echo base_url()?> resources / uploadify / uploadify.swf',
'script':'<?php echo site_url()?> / managerupload / vehicle_imageUpload',
'cancelImg':'<?php echo base_url ()?> resources / uploadify / cancel.png',
'fileExt':'* .jpg; *。jpeg; *。png; *。gif',
'folder':' none,
'auto':true,
'multi':false,
'scriptData':{'vehicleID':'<?php echo $ vehicleID?>
'onComplete':function(){vImg_reload('<?php echo $ vehicleID?>');}
});});

< / script>

scriptData配置选项通过POST传递。



注意:onComplete config选项允许您执行函数;在这种情况下,是更新当前车辆的图像的Ajax调用;这可能或可能不与你相关。让我知道如果是,我会发布。

  function vehicle_imageUpload(){
$ this-> _checklogin();

$ file_dir =vehicle_images /;

$ config ['upload_path'] ='./vehicle_images/';
$ config ['allowed_types'] ='gif | jpg | png';
$ config ['max_size'] ='1000000';
$ config ['overwrite'] = TRUE;
$ config ['remove_spaces'] = TRUE;
$ config ['encrypt_name'] = FALSE;

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

if(!$ this-> upload-> do_upload('Filedata')){
echo $ this-> upload-> display_errors
} else {

$ errors = $ this-> upload-> display_errors();

$ upload_info = $ this-> upload-> data();

//将文件信息插入数据库
$ insert_data = array(
'vImg_vehicle_id_fk'=> $ this-> input-> post('vehicleID'
'vImg_filename'=> $ upload_info ['file_name'],
'vImg_filepath'=> $ upload_info ['file_path'],
'vImg_primary'=> 1,
'vImg_directory'=> $ file_dir
);
$ this-> db-> insert('vehicle_images',$ insert_data);
}
}

这是使用CI上传类的标准上传处理程序;这里我们使用传递的POST项vehicleID将图像的记录插入数据库。



与所有闪光灯的东西,你必须解决 Flash Cookie Bug 。有两个选项,通过Uploadify的'scriptData'数组传递会话ID(如上所述),并使用现有会话ID从上传控制器重新加载会话。另一个选项是没有Codeigniter匹配会话useragent。



在application / config.php

  $ config ['sess_match_useragent'] = FALSE; 

这会阻止会话类在看到不属于会话的Useragent时破坏会话。



如果你担心这个的安全影响,你只需要记住,用户代理是微不足道的假的,只要你现有的安全性,没有问题。


How can i implement this in codeigniter?Uploadify(Jquery)

I mean what will be the controller? Hows the progress tracked? And what will happen if flashplayer is not installed by the user?

And How do i check progess? Have any one tried this with codeigniter?

解决方案

I have successfully implemented uploadify in codeigniter as follows:

In the header view:

<script type="text/javascript" src="<?php echo base_url()?>resources/js/jquery.js"></script>
<link rel="stylesheet" type="text/css" media="all" href="<?php echo base_url()?>resources/uploadify/uploadify.css" />
<script type="text/javascript" src="<?php echo base_url()?>resources/uploadify/swfobject.js"></script>
<script type="text/javascript" src="<?php echo base_url()?>resources/uploadify/jquery.uploadify.v2.1.0.min.js"></script>

In the page view:

<input id="fileInput" name="fileInput" type="file" />
 <script type="text/javascript">
    $(document).ready(function() {
    $("#fileInput").uploadify({
        'uploader'       : '<?php echo base_url()?>resources/uploadify/uploadify.swf',
        'script'         : '<?php echo site_url()?>/managerupload/vehicle_imageUpload',
        'cancelImg'      : '<?php echo base_url()?>resources/uploadify/cancel.png',
        'fileExt'        : '*.jpg;*.jpeg;*.png;*.gif',
        'folder'         : '/nonexistant',
        'auto'           : true,
        'multi'          : false,
        'scriptData'     : {'vehicleID': '<?php echo $vehicleID?>'},
        'onComplete'     : function() {vImg_reload('<?php echo $vehicleID?>');}
    });    });

</script>

The scriptData config option is passed through POST.

NOTE: the onComplete config option allows you to execute a function; in this case it is an Ajax call to update the images for the current vehicle; this may or may not be relevant to you. Let me know if it is and i will post it. As it is, i would rather not muddy the waters.

In the controller:

function vehicle_imageUpload(){
    $this->_checklogin();

    $file_dir = "vehicle_images/";

    $config['upload_path'] = './vehicle_images/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '1000000';
    $config['overwrite'] = TRUE;
    $config['remove_spaces'] = TRUE;
    $config['encrypt_name'] = FALSE;

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

    if(! $this->upload->do_upload('Filedata')){
        echo $this->upload->display_errors();
    }else{

       $errors = $this->upload->display_errors();

       $upload_info = $this->upload->data();

       // Insert file information into database
       $insert_data = array(
        'vImg_vehicle_id_fk' => $this->input->post('vehicleID'),
        'vImg_filename' => $upload_info['file_name'],
        'vImg_filepath' => $upload_info['file_path'],
        'vImg_primary' => 1,
        'vImg_directory' => $file_dir
       );
       $this->db->insert('vehicle_images', $insert_data);
      }
}

This is a standard upload handler using the CI upload class; here we used the passed POST item 'vehicleID' to insert a record of the image into the database.

As with all flash stuff you have to work around the Flash Cookie Bug. There are two options, pass your session id through Uploadify's 'scriptData' array (as described above), and reload the session from the upload controller with the existing session ID. The other option is to not have Codeigniter match the session useragent.

In application/config.php

$config['sess_match_useragent'] = FALSE;

This prevents the Session Class from destroying the session when it sees a Useragent that doesnt belong to the session.

If you are worried about the security implications of this you just need to remember that user agents are trivial to fake and so long as your existing security is soundly implemented you will have no problems.

这篇关于实现jquery上传插件'uploadify'与codeigniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆