文件上传

CmsTop系统中的文件上传是结合附件管理完成的,每一个上传的图片都可以在附件管理功能中管理。 前台的文件上传使用了自主研发的Flash上传系统,通过简单的配置即可完成定制性的批量上传功能。

下面通过一个简单的实例来展示上传图片的处理过程。

前端代码

<!-- 引入flash上传框架-->
<script type="text/javascript" src="uploader/cmstop.uploader.js"></script>
<!--
使用Jqueryuploader绑定上传事件-->
<script type="text/javascript">
$(function(){
            $("#uploader").uploader({
                                   script               : '?app=test&controller=index&action=upload',         //
上传文件接收地址
                                   fileDataName   : 'picdata',                                                       //
表单上传图片字段名
                                   fileDesc            : '
图片',                                                //文件描述
                                   fileExt              : '*.jpg;*.gif;*.png;',                                          //
允许的文件后缀
                                   buttonImg       :'images/multiup.gif',                                       //
上传按钮显示的图片
                                   sizeLimit           : 10485760,                             //
允许上传的图片尺寸,10M,以byte为单位
                                   multi:false,                                                                              //
是否同时上传多图
                                   complete:function(response,data){                                          //
上传成功的回调函数
                                               response =(new Function("","return "+response))();
                                               if(response.state) {
                                                           ed || (ed = tinyMCE.get('content'));
                                                           ed.execCommand('mceInsertContent', false, response.code);
                                                           ct.ok(response.msg);
                                               } else {
                                                           ct.error(response.msg);
                                               }
                                   }
            });
});
</script>
<!--
HTML中写一个uploader按钮-->
<div id="uploader" class="uploader"></div>

后端处理

function upload()
{
            $attachment = loader::model('admin/attachment', 'system');
            $file = $attachment->upload('picdata',true, null,'jpg|png|gif',$this->upload_max_filesize,array());
            if ($file)
            {
                        $result = array('state'=>true,'file'=>$file,'msg'=>'
上传成功');
            }
            else
            {
                        $msg = is_array($attachment->error) ? implode('', $attachment->error) : $attachment->error;
                        $result = array('state'=>true,'file'=>'','msg'=>$msg);
            }
            exit($this->json->encode($result));
}

关于上传图片的更多用法,前端请参考“JS指南-类库-图片上传,后端请参考“system应用的admin/attachment数据模型



Copyright ©2009 - 2014 CmsTop.Com.All rights reserved.
思拓合众