thinkPHP+uploadify 实现图片上传预览 ,小弟新手,做的不足的地方望指教!
效果图
模板文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="/Public/jquery.uploadify-v2.1.0/example/css/default.css" rel="stylesheet" type="text/css" />
<link href="/Public/jquery.uploadify-v2.1.0/uploadify.css" rel="stylesheet" type="text/css" />
<script charset="utf-8" type="text/javascript" src="/Public/jquery.uploadify-v2.1.0/jquery-1.3.2.min.js"></script>
<script charset="utf-8" type="text/javascript" src="/Public/jquery.uploadify-v2.1.0/swfobject.js"></script>
<script charset="utf-8" type="text/javascript" src="/Public/jquery.uploadify-v2.1.0/jquery.uploadify.v2.1.0.min.js"></script>
<style type="text/css">
#image{
height:300px;
width:720px;
border:1px #969594 solid;
}
</style>
<title>图片上传</title>
</head>
<body>
<form id="form1" enctype="multipart/form-data" >
<div id="fileQueue" style='height:200px;display:none'></div>
<input type="file" name="uploadify" id="uploadify" multiple="true" />
</div>
</form>
<div id="image" ></div>
<script type="text/javascript">
$(document).ready(function () {
$("#uploadify").uploadify({
'uploader':'/Public/jquery.uploadify-v2.1.0/uploadify.swf', //swf路径
'script': '/Code/file', //后台处理文件上传的路径
'cancelImg':'/Public/jquery.uploadify-v2.1.0/cancel.png', //按钮背景图片的路径
'folder': '/Uploads',
'method':'post',
'buttonText':'file',
'fileExt': '*.jpg;*.gif,*.png', //允许上传的文件格式为*.jpg,*.gif,*.png
'fileDesc': 'Web Image Files(.JPG,.GIF,.PNG)', //过滤掉除了*.jpg,*.gif,*.png的文件
'queueID': 'fileQueue',
'sizeLimit': '2048000', //最大允许的文件大小为2M
'fileDataName':'uploadify',
'auto': false,
'queueSizeLimit':15,
'simUploadLimit':15,
'removeCompleted':false,
'multi':true,
'onCancel': funCancel, //当用户取消上传时
'onComplete': funComplete, //完成上传任务
'OnError': funError //上传发生错误时
});
});
//用户取消函数
function funCancel(event, ID, fileObj, data) {
alert('您取消了操作');
return;
}
//图片上传发生的事件
function funComplete(event, ID, fileObj, response, data) {
//alert('上传事件');
if (response == 0) {
alert('图片' + fileObj.name + '操作失败');
return false;
}else{
var str=$('#image').html();
var add="<img src='"+"/Uploads/"+response+"'" +"style='margin-left:15px;margin-top:15px'/></img>";
str+=add;
$('#image').html(str);
return true;
}
}
//上传发生错误时。
function funError(event, ID, fileObj, errorObj) {
//alert('错误事件');
alert(errorObj.info);
return;
}
</script>
<a href="javascript:$('#uploadify').uploadifyUpload()">上传</a>|
</body>
</html>
控制器类:
<?php
/*
* 新闻类
*/
class ArticleAction extends Action{
//
/*public function load()
{
import('@.ORG.editor');
$ob=new editor("800px",'400px','editor');
$str=$ob->userJs();
$editor=$ob->createEditor();
$this->assign("str",$str);
$this->assign("editor",$editor);
$this->display();
}
//
public function php()
{
import('@.ORG.editor');
$ob=new editor( );
$content=$ob->getEditorContent();
$this->assign('content',$content);
$this->display();
} */
public function file()
{
//加载文件上传,图片处理类
import("@.ORG.UploadFile");
//导入上传类
$upload = new UploadFile();
//设置上传文件大小
$upload->maxSize=3292200;
//设置上传文件类型
$upload->allowExts=explode(',','jpg,gif,png,jpeg');
//设置附件上传目录
$upload->savePath='./Uploads/';
//设置需要生成缩略图,仅对图像文件有效
$upload->thumb = true;
// 设置引用图片类库包路径
$upload->imageClassPath ='@.ORG.Image';
//设置需要生成缩略图的文件后缀
$upload->thumbPrefix='m_,s_'; //生产2张缩略图
//设置缩略图最大宽度
$upload->thumbMaxWidth='400,100';
//设置缩略图最大高度
$upload->thumbMaxHeight='400,100';
//设置上传文件规则
$upload->saveRule='uniqid';
//删除原图
$upload->thumbRemoveOrigin=true;
if( !$upload->upload() )
{
echo '0';
}else{
$info=$upload->getUploadFileInfo();
$src='s_'.$info[0]['savename'];
echo $src;
}
}
public function preview ()
{
$this->display();
}
}