首先是js处理部分:
$("#touch_icon").uploadify({
//file_path_name 是分类保存图片的文件夹名
//type 值可以自定义,传到控制器
'formData' : {'file_path_name' : 'touch_icon','type' : '1'},
'width' : 104,
'height' : 30,
'swf' : SCOPE.uploadify_swf,
'uploader' : SCOPE.uploadify_url,
'buttonText' : '',
'fileTypeDesc' : '手机小图标',
'fileObjName' : 'images',
'method' : 'get',
'onUploadSuccess' : function(file, data, response) {
if(response)
{
var obj = JSON.parse(data);
$("#touch_img").find("a").attr("href", obj.data);
$("#touch_img").find(".icon-picture").attr("onmouseover", "toolTip('<img src= " + obj.data + ">')");
$("#t_icon").attr("value", obj.data);
$("#touch_img").attr("class", "show");
}
},
});简单点就是自定义了两件参数file_path_name和type抛送给了控制器, 'swf' : SCOPE.uploadify_swf,
'uploader' : SCOPE.uploadify_url,
这两个也是接收模版文件抛送过来的地址<script type="text/javascript">
var SCOPE={
'uploadify_swf':"地址",
'uploadify_url' :"地址",
}控制器代码:namespace app\api\controller;
use think\Controller;
use think\Request;
use think\File;
class Images extends Controller
{
//商品分类手机小图标上传
public function upload()
{
//获取表单上传文件
$file = request()->file('images');
//获取get传送过来的参数
$getData = input('get.');
if ($getData['type'] == 1)
{
// 自定义规则生成文件名,并移动到框架应用根目录/data/uploads/file_path_name 目录下
$info = $file->rule('uniqid')->move('data' . DS . 'uploads' . DS . $getData['file_path_name']);
}
elseif ($getData['type' == 2])
{
// 移动到框架应用根目录/data/uploads/file_path_name/日期 目录下
$info = $file->move('data' . DS . 'uploads' . DS . $getData['file_path_name']);
}
if($info && $info->getPathname())
{
//将'\'替换成'/'后返回 data/uploaps/file_path_name/日期/文件名.jpg
return json(show('1','success','/' . str_replace('\\', '/', $info->getPathname())));
}
return json(show('0','error'));
}
}show方法/**
* 显示
* @param [type] $status 状态
* @param [type] $message 提示
* @param array $data 数组数据
*/
function show($status, $message = '', $data = [])
{
return [
'status' => intval($status),
'message' => $message,
'data' => $data,
];
} 最佳答案