下载文件
首先前往 //html5-make-upload-thumb
下载所需文件。注:如果你安装git,那么可以直接在你本地
git clone //html5-make-upload-thumb
如果没有安装,直接下载压缩包即可!
引入文件
引入文件顺序可改变,不过必须先引入Jquery
html端处理
javascript处理$(function(){var$form=$('#j-form');var$file=$('#j-file');var$status=$('.j-status');$file.makeThumb({width:400,height:400,//mark:{padding:5,src:'mark.png',width:30,height:30},success:function(dataURL,tSize,file,sSize,fEvt){//postdatavar$up=$('');$up.insertAfter($file).val(dataURL);$file.remove();varload=layer.open({type:2,content:'上传中'});$.ajax({url:"api.php",data:$form.serialize(),method:"POST",async:true,dataType:"json",success:function(data){layer.close(load);$("#ret").html(data.msg);console.log(data);}});//$form.submit();}});})
php端处理
phpheader('Content-Type:text/html;charset=UTF-8');//saveimg$img=$_POST['base64'];if(isset($img)){sleep(5);//休息几秒//#dataURIbase_64编码上传手机端常用方式$rootPath='./face/'.date('Ymd');$target=$rootPath."/".date('Ymd')."_".substr(md5(rand(1,10000)),0,6).".png";if(preg_match('/data:([^;]*);base64,(.*)/',$img,$matches)){$img=base64_decode($matches[2]);if(!file_exists($rootPath)){mkdir($rootPath,0777,true);}if(file_put_contents($target,$img)){$str="
文件来自api返回:
";$str.="";exit(json_encode(array('status'=>1,'msg'=>$str)));}else{exit(json_encode(array('status'=>0,'msg'=>'文件上传失败')));}}else{//普通上传这种方式一般不会用(原始上传方式)$uploadFile=$_FILES['upfile'];$target='tmp1.jpg';if(isset($uploadFile)&&is_uploaded_file($uploadFile['tmp_name'])&&$uploadFile['error']==0){echo'filename:'.$uploadFile['name'].',';echo'type:'.$uploadFile['type'].',';echo'size:'.($uploadFile['size']/1024).'Kb';move_uploaded_file($uploadFile['tmp_name'],$target);}else{echo'error:'.$uploadFile['error'];}}}