武汉php大巴吧 关注:13贴子:63
  • 0回复贴,共1

uploadify 封装使用方法

取消只看楼主收藏回复

一,封装方法 页面(uploadify.tpl)<script type="text/javascript" src="{{$baseUrl}}/www/common/uploadify/jquery.uploadify.min.js"></script>
<link rel="stylesheet" type="text/css" href="{{$baseUrl}}/www/common/uploadify/uploadify.css"/>
<script type="text/javascript">
var arrayImgObj = new Array();
var public_uploadifyname = new Array();
function info_uploadify(uploadifyname,width,height,auto,uploader,method,buttonText,multi,queueSizeLimit,fileTypeExts,fileSizeLimit,thum,thum_width,thum_height,functions){
$(uploadifyname).uploadify({
'height': height, //上传按钮的高和宽
'width': width,
'auto' : auto, //选定文件后是否自动上传,默认false
'swf' : '/www/common/uploadify/uploadify.swf?ver=' + Math.random(),
'uploader' : '{{$img_upload_url}}',//后台上传function 参数 uploader
// 'uploader' : '/uploadify/uploadify',//后台上传function 参数 uploader
'method' : method, //post get 传输方式
'buttonText' : buttonText, //设置按钮文本
'multi' : multi, //允许同时上传多张图片
// 'formData' :{'thum':thum,'thum_width':thum_width,'thum_height':thum_height},//后台传参 占时主要用于缩略图设置 thum是否创建缩略图 1->是 ,0->否
'queueSizeLimit' : queueSizeLimit, //最多一次上传几张
'fileTypeDesc' : 'Image Files', //只允许上传图像
'fileTypeExts' : fileTypeExts, //限制允许上传的图片后缀'*.TIFF; *.tiff; *.gif; *.GIF; *.jpg; *.JPG; *.PNG; *.BMP; *.bmp; *.JPEG; *.jpeg; *.psd; *.PSD; *.png'
'fileSizeLimit' : fileSizeLimit,//限制上传的图片大小 '100kb'
'onUploadStart' : function(file) {
//选中图片上传之前调用函数
},
'onUploadSuccess' : function(file, data, response) { //每次成功上传后执行的回调函数,从服务端返回数据到前端
var ImgObj = eval('(' + data + ')');
public_uploadifyname['uploadifyname'] = uploadifyname;
public_uploadifyname['filename'] = file.name;
if(ImgObj['id'] != 0){
arrayImgObj.push(ImgObj);
}
functions();
},
'onUploadError' : function(file,errorCode,errorMsg,errorString,swfuploadifyQueue) { //上传文件出错是触发(每个出错文件触发一次)
alert(' - 错误代码: ' + errorCode+ ' - 错误描述: ' + errorMsg+ ' - 简要错误描述: ' + errorString);
}
});
}
function info_uploadifys(uploadifyname,width,height,auto,uploader,method,buttonText,multi,queueSizeLimit,fileTypeExts,fileSizeLimit,thum,thum_width,thum_height,functions){
$(uploadifyname).uploadify({
'height': height, //上传按钮的高和宽
'width': width,
'auto' : auto, //选定文件后是否自动上传,默认false
'swf' : '/www/common/uploadify/uploadify.swf?ver=' + Math.random(),
'uploader' : '{{$img_upload_url}}',//后台上传function 参数 uploader
'method' : method, //post get 传输方式
'buttonText' : buttonText, //设置按钮文本
'multi' : multi, //允许同时上传多张图片
// 'formData' :{'thum':thum,'thum_width':thum_width,'thum_height':thum_height},//后台传参 占时主要用于缩略图设置 thum是否创建缩略图 1->是 ,0->否
'queueSizeLimit' : queueSizeLimit, //最多一次上传几张
'fileTypeDesc' : 'Image Files', //只允许上传图像
'fileTypeExts' : fileTypeExts, //限制允许上传的图片后缀'*.TIFF; *.tiff; *.gif; *.GIF; *.jpg; *.JPG; *.PNG; *.BMP; *.bmp; *.JPEG; *.jpeg; *.psd; *.PSD; *.png'
'fileSizeLimit' : fileSizeLimit,//限制上传的图片大小 '100kb'
'onUploadStart' : function(file) {
//选中图片上传之前调用函数
},
'onUploadSuccess' : function(file, data, response) { //每次成功上传后执行的回调函数,从服务端返回数据到前端
var ImgObj = eval('(' + data + ')');
if(ImgObj['id'] != 0){
arrayImgObj.push(ImgObj);
}
public_uploadifyname['uploadifyname'] = uploadifyname;
public_uploadifyname['filename'] = file.name;
},
'onQueueComplete': function(queueData){
functions();
},
'onUploadError' : function(file,errorCode,errorMsg,errorString,swfuploadifyQueue) { //上传文件出错是触发(每个出错文件触发一次)
alert(' - 错误代码: ' + errorCode+ ' - 错误描述: ' + errorMsg+ ' - 简要错误描述: ' + errorString);
}
});
}
</script>
二,使用方法加载 {{include file="uploadify.tpl"}}<script type="text/javascript">
info_uploadify('#test',84,30,true,"{{$baseUrl}}/uploadify/uploadify","post","上传图片",true,1,"*.gif; *.GIF; *.jpg; *.JPG; *.PNG;","200kb",0,0,0,successFunction);
function successFunction(){
var count = arrayImgObj.length;
var img_url = arrayImgObj[count-1].org;
var img_id = arrayImgObj[count-1].id;
$("input[name='img_url']").val(img_url);
$("input[name='img_id']").val(img_id);
$("#right-img").attr("src",img_url);
$("#img_url").attr("src",img_url);
}
</script> 三,控制器保存<?php
class UploadifyController extends My_Controller
{
public $check_access=false;
/**
*
* 上传视频 音乐 等非图片类型文件
*/
function uploadifyvideoAction() {
$pic = new PicData();
$tempFile = $_FILES['Filedata']['tmp_name'];
$extend = explode("." , $_FILES['Filedata']['name']);
$va=count($extend)-1;
$path = $_SERVER['SINASRV_DATA_TMP'].'/video'.time().mt_rand(1000000000,9999999999).".".$extend[$va];
move_uploaded_file($tempFile,$path);
$ret = $pic->post_file($path,1);
$pic->close();
if(empty($ret['id'])){
$ret['id'] = 0;
}
echo json_encode($ret);
}
function uploadifyAction() {
$pic = new PicData();
$tempFile = $_FILES['Filedata']['tmp_name'];
$extend = explode("." , $_FILES['Filedata']['name']);
$va=count($extend)-1;
$path = $_SERVER['SINASRV_DATA_TMP'].'/'.time().mt_rand(1000000000,9999999999).".".$extend[$va];
move_uploaded_file($tempFile,$path);
$ret = $pic->post_file($path);
$pic->close();
if(empty($ret['id'])){
$ret['id'] = 0;
}
echo json_encode($ret);
// $thum = isset($_REQUEST["thum"])?$_REQUEST["thum"]:0;
// $thum_width = isset($_REQUEST["thum_width"])?$_REQUEST["thum_width"]:0;
// $thum_height = isset($_REQUEST["thum_height"])?$_REQUEST["thum_height"]:0;
//
// $ret = $pic->upload_file($tempFile,$thum,$thum_width,$thum_height);
// $ret['org'] = constant("IMG_DOMAIN").$ret['org'];
// if(isset($ret['thum']) && !empty($ret['thum'])){
// $ret['thum'] = constant("IMG_DOMAIN").$ret['thum'];
// }else{
// $ret['thum'] = '';
// }
//echo json_encode($ret);
// $targetFolder = './www/uploadify/images'; //设置图标上传目录
// if (!empty($_FILES)) {
// $tempFile = $_FILES['Filedata']['tmp_name'];
// $extend = explode("." , $_FILES['Filedata']['name']);
// $va=count($extend)-1;
// $path = time().mt_rand(100000,999999).".".$extend[$va];
// $targetFile =$targetFolder. '/' . $path; //$_FILES['Filedata']['name'];//上传后的图片路径
// $fileTypes = array('jpg','jpeg','gif','png','JPG','JPEG','GIF','PNG'); //允许的文件后缀
// $fileParts = pathinfo($_FILES['Filedata']['name']);
//
// if (in_array($fileParts['extension'],$fileTypes)) {
//
// move_uploaded_file($tempFile,iconv("UTF-8","gb2312", $targetFile));
// $picdata = new WxPicData();
// $data['org'] = $targetFile;
// $data['cdate'] = strtotime(date("Y-m-d H:i:s"));
// $data = $picdata->prepareData($data);
// $imageid = $picdata->insert_data($data);
// echo json_encode($imageid);
//
// } else {
// echo json_encode('上传失败');
// }
//
// }
}
function alert($msg) {
header('Content-type: text/html; charset=UTF-8');
$json = new Services_JSON();
echo $json->encode(array('error' => 1, 'message' => $msg));
exit;
}
//剪裁图片上传回调
function cutuploadifyAction() {
$pic=new PicData();
$tempFile = $_FILES['Filedata']['tmp_name'];
$ret=$pic->upload_file($tempFile,0,0,0);
$ret['org'] = constant("IMG_DOMAIN").$ret['org'];
if(isset($ret['thum']) && !empty($ret['thum'])){
$ret['thum'] = constant("IMG_DOMAIN").$ret['thum'];
}else{
$ret['thum'] = '';
}
$img_info = getimagesize($ret['org']);
$ret['uploadheight']=$img_info[1];//上传图片的高度
$ret['uploadwidth']=$img_info[0];//上传图片的宽度
echo json_encode($ret);
}
//手机
function uploadifytelAction() {
$pic = new PicData();
$tempFile = $_FILES['fileimg']['tmp_name'];
$thum = isset($_REQUEST["thum"])?$_REQUEST["thum"]:0;
$thum_width = isset($_REQUEST["thum_width"])?$_REQUEST["thum_width"]:0;
$thum_height = isset($_REQUEST["thum_height"])?$_REQUEST["thum_height"]:0;
$ret = $pic->upload_file($tempFile,$thum,$thum_width,$thum_height);
$ret['org'] = constant("IMG_DOMAIN").$ret['org'];
if(isset($ret['thum']) && !empty($ret['thum'])){
$ret['thum'] = constant("IMG_DOMAIN").$ret['thum'];
}else{
$ret['thum'] = '';
}
$img_info = getimagesize($ret['org']);
$ret['uploadheight']=$img_info[1];//上传图片的高度
$ret['uploadwidth']=$img_info[0];//上传图片的宽度
echo json_encode($ret);
}
//上传音乐
function uploadifymusicAction(){
if(!is_dir($_SERVER["SINASRV_UPLOAD"]."/music/")){
mkdir($_SERVER["SINASRV_UPLOAD"]."/music/");
}
$tempFile = $_FILES['Filedata']['tmp_name'];
$ext_name = strrchr($_FILES['Filedata']['name'], '.');
$ext_name = strtolower($ext_name);
if($ext_name=='.wav' || $ext_name=='.mp3' || $ext_name=='.wma' || $ext_name=='.ogg' || $ext_name=='.ape'|| $ext_name=='.acc'){
$root = $_SERVER["SINASRV_UPLOAD"]."/music/".date('dHis').substr( implode(NULL,array_map('ord',str_split(substr(uniqid(),8,13),1))) , 0 , 8).date('m').$ext_name;
if(is_uploaded_file($tempFile))
{
$up_status=move_uploaded_file($tempFile,$root);
}
else
{
$up_status=copy($tempFile,$root);
}
if(!$up_status){
$return_info['result']=0;
//$this->write_log('copy_org->上传原图失败(src:'.$this->src_file.',df->'.$df.')');
}
else
{
$pic = new PicData();
$return_info['result']=1;
$data['org'] = str_replace($_SERVER["SINASRV_UPLOAD"],'',$root);
$return_info["id"] = $pic->insert_data($data);
$return_info["root"] = str_replace($_SERVER["SINASRV_UPLOAD"],'',$root);
}
}else{
$return_info['result']=0;
}
echo json_encode($return_info);
}
//返回本地的上传文件的路径:2014-8-19
function newuploadifyAction(){
$rootname = isset($_GET['rootname'])?$_GET['rootname']:'0';//获取保存的另外的文件名,没有就使用默认路径
if($rootname=='0'){
$path = $_SERVER['SINASRV_DATA_TMP'].'/';
}else{
$path = $_SERVER['SINASRV_DATA_TMP']."/".$rootname."/";
}
$tempFile = $_FILES['Filedata']['tmp_name'];
$extend = explode("." , $_FILES['Filedata']['name']);
$va=count($extend)-1;
if(!is_dir($path)){
mkdir($path);
}
$filepath = $path.time().mt_rand(10000,99999).".".$extend[$va];
move_uploaded_file($tempFile,$filepath);
if($rootname=='mobilevcard'){
//转成正的图片
$filepath = Vcard::reverserightpic($filepath);
//生成小点的图
$img_info = @getimagesize($filepath);
if($img_info==false){
}else{
$filepath = Vcard::thumbAction(0,0,$img_info[0],$img_info[1],$filepath,120,120);
}
}elseif($rootname=='clubcard'){
//转成正的图片
$filepath = Vcard::reverserightpic($filepath);
}
$pic=new PicData();
$ret=$pic->post_file($filepath);
// var_dump($filepath);
// var_dump($ret['org']);
$img_info = @getimagesize($ret['org']);
// var_dump($img_info);
if($img_info==false){
$img_info[0] = 0;
$img_info[1] = 0;
}
$response = array(
'id' => $ret['id'],
'org' => $ret['org'],
'oldorg' => $ret['org'],//$filepath
'uploadheight' => $img_info[1],
'uploadwidth' => $img_info[0],
);
echo json_encode($response);
//echo json_encode(array('id' => $ret['id'], 'org' => $filepath);
}
}


IP属地:湖北1楼2015-10-23 14:56回复