怎么在Thinkphp中利用smarty与uploadify实现一个无刷新上传功能?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
模板文件代码:
<!DOCTYPE html>
<html lang="cn">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="<{$smarty.const.PUBLIC_PATH}>/Uploadify/uploadify.css" rel="stylesheet" type="text/css" />
<script src="<{$smarty.const.PUBLIC_PATH}>/Uploadify/jquery.js" type="text/javascript"></script>
<script src="<{$smarty.const.PUBLIC_PATH}>/Uploadify/jquery.uploadify.min.js" type="text/javascript"></script>
</head>
<script type="text/javascript">
$(function() {
$("#file_upload").uploadify({
//指定swf文件
'swf': '<{$smarty.const.PUBLIC_PATH}>/Uploadify/uploadify.swf',
//后台处理的页面
'uploader': "<{U('home/Login/Uploads','',false)}>",
//按钮显示的文字
'buttonText': '上传图片',
//显示的高度和宽度
"height" : 30,
'fileTypeDesc': 'Image Files',
//允许上传的文件后缀
'fileTypeExts': '*.gif; *.jpg; *.png',
//发送给后台的其他参数通过formData指定
//'formData': { 'someKey': 'someValue', 'someOtherKey': 1 },
"method" : 'post',//方法,服务端可以用$_POST数组获取数据
'removeTimeout' : 1,
"onUploadSuccess" : uploadPicture
});
//可以根据自己的要求来做相应处理
function uploadPicture(file, data){
var data = eval('(' + data + ')');
if(data.errorcode){
alert(data.errormsg);
} else {
alert(data.errormsg);
}
}
});
</script>
<body>
<input type="file" name="file_upload" id="file_upload" />
</body>
</html>
控制器代码:
public function uploads(){
$arr = array( "errorcode"=>"1","errormsg"=>"上传成功!");
$model = M('applicant');
if (!empty($_FILES)) {
//图片上传设置
$config = array(
'maxSize' => 1000000,
'rootPath' => 'Public',
'savePath' => '/Uploads/',
'saveName' => array('uniqid',''),
'exts' => array('jpg', 'gif', 'png', 'jpeg'),
'autoSub' => false,
'subName' => array('date','Ymd'),
);
$upload = new \Think\Upload($config);// 实例化上传类
$info = $upload->upload();
if($info){
$arr['errorcode'] = "0";
} else {
$arr["errorcode"] = "1";
$arr["errormsg"] = $upload->getError();
}
/* 返回JSON数据 */
$this->ajaxReturn($arr);
}
}
关于怎么在Thinkphp中利用smarty与uploadify实现一个无刷新上传功能问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。