1.首先需要安装一下uploadify
(由于我是使用在admin模块用来上传图文混合的内容,我在tp5下面appliction->public->static下新建了admin模块用来管理admin模块下一些前端所使用的插件,具体上传的uploadify请看下图)

2.在public->static->admin下建立一个js目录并新建mage.js的文件,用来存放公共图片上传js的方法,下面为image.js里面的加入的方法。
$(function(){
$("#file_upload").uploadify({
swf : swf,
uploader : image_upload_url,
buttonText : '图片上传',
fileTypeDesc: 'Image files',
fileObjName: 'file',
fileTypeExts: '*.gif;*.jpg;*.png',
onUploadSuccess: function(file, data, response){
if(response){
var obj = JSON.parse(data);
$('#upload_org_code_img').attr("src", obj.data);
$('#file_upload_image').attr("value", obj.data);
$('#upload_org_code_img').show();
}
}
});
});
3.在使用的页面导入相关的js,css文件
<link rel="stylesheet" type="text/css" href="__STATIC__/admin/uploadify/uploadify.css" />
{load href="__STATIC__/admin/uploadify/jquery.uploadify.min.js" /}
{load href="__STATIC__/admin/js/image.js" /}
4.在HTML 的body使用区域加上该控件
<input id="file_upload" type="file" multiple="true" >
<img style="display: none" id="upload_org_code_img" src="" width="150" height="150">
<input id="file_upload_image" name="image" type="hidden" multiple="true" value="">
5.并在该页面的scriprt的区域加上一些常量的定义
<script>
swf = '__STATIC__/admin/uploadify/uploadify.swf';
image_upload_url = "{:url('image/upload')}"; //这个关键是调用php 的image类下的upload方法
</script>
6.在控制器下加入 upload 的方法的具体处理细节
public function upload(){
$file = Request::instance()->file('file');
//把图片上传到指定的文件夹中
$info = $file->move('upload');
if($info && $info->getPathname()){
$data = [
'status' => 1,
'message' => 'OK',
'data' => '/'.$info->getPathname(),
];
echo json_encode($data);exit;
}
echo json_encode(['status' => 0, 'message' => '上传失败']);
}
网友评论