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