当前位置: 移动技术网 > IT编程>开发语言>PHP > Thinkphp+smarty+uploadify实现无刷新上传

Thinkphp+smarty+uploadify实现无刷新上传

2018年05月07日  | 移动技术网IT编程  | 我要评论

本文实例讲述了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);
  }
}

希望本文所述对大家的php程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网