使用tp3.2和mbUploadify.js上传图片的代码,记录一下
HTML:
<div class="form-group">
<label class="col-sm-1 control-label no-padding-right" for="form-field-4"> 图片: </label>
<div class="col-sm-9">
<input type="file" name="files" id="imgfile" multiple style="display:none;" onchange = "imgpath.value=this.value" >
<input type="textfield" id="imgpath" style="border: 0px;outline:none;cursor: pointer;width:100px;display:none;">
<input type="button" class="btn btn-white btn-info btn-sm" value="点击上传图片" onclick="imgfile.click()">
<div class="space-4"></div>
<div id="img-data" class="img-data">
<if condition="$data['savepath'] neq ''">
<span class="uploadimg">
<img src="{$data['savepath']}" style="max-width: 300px;">
<input type="hidden" name="img" value="{$data['img']}">
<a class="remove-uploadimg" title="删除">✕</a>
</span>
</if>
</div>
<div class="space-4"></div>
<div id="imgError" class="msg"></div>
</div>
</div>
CSS:
<style>
.remove-uploadimg{ cursor:pointer;}
.uploadimg{
display: inline-block;
position: relative;
}
.uploadimg .remove-uploadimg{
position: absolute;
font-size: 20px;
top:-10px;
right: -6px;
}
.remove-uploadimg{
width:30px;
height:30px;
background-color:#ccc;
border-radius:50%;
color:red;
text-align:center;
}
.remove-uploadimg:hover{
text-decoration: none;
}
</style>
JS:
<script src="__PUBLIC__/js/mbUploadify.js"></script>
<script>
var upload1 = new mbUploadify({
file: document.getElementById('imgfile'),
/*ajax 上传地址*/
url: "{:U('Upload/mbUploadImg')}",
//上传进度
progress: function(){
$('#imgpath').show();
$('#imgpath').val('上传中...');
},
/*上传失败*/
error: function(file, msg){
document.getElementById('imgError').innerHTML = msg;
},
/*ajax上传成功*/
uploadSuccess: function(res){
$('#imgpath').hide();
$('#imgpath').val('');
var data = JSON.parse(res);
document.getElementById('img-data').innerHTML = '<span class="uploadimg">' +
'<img src="'+ data.savepath +'" style="max-width: 300px;">' +
'<input type="hidden" name="img" value="'+data.id+'">'+
'<a class="remove-uploadimg" title="删除">✕</a>' +
'</span>';
}
});
$('body').on('click','.remove-uploadimg',function(){
$(this).parents('.uploadimg').remove();
})
</script>
PHP:
public function mbUploadImg(){
$upload = new Upload(); // 实例化上传类
$upload->maxSize = 5242880 ; // 设置附件上传大小
$upload->exts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
$upload->rootPath = './Public/';
// 上传文件
$info = $upload->uploadOne($_FILES['files']);
if($info) {
// 上传成功
$data['name'] = $info['name'];
$data['ext'] = $info['ext'];
$data['type'] = $info['type'];
$data['savename'] = $info['savename'];
$data['savepath'] = "/Public/".$info['savepath'].$info['savename'];
$imgId = M('upload_img')->add($data);
if($imgId){
$resData['code'] = 200;
$resData['msg'] = '成功';
$resData['id'] = $imgId;
$resData['name'] = $data['name'];
$resData['savepath'] = $data['savepath'];
echo json_encode($resData);
return;
}
}
// 上传错误提示错误信息
return $this->ajaxReturn(['code'=>400,'msg'=>$upload->getError()]);
}
使用tp3.2和mbUploadify.js上传图片的代码,记录一下的更多相关文章
- c#使用js上传图片
前几天朋友说用js上传图片过去遇到点问题,于是自己也想写一个demo这里就把自己挖的坑填了. 话不多说上代码 前台就一个file控件加按钮 <!DOCTYPE html> <html ...
- 富文本编辑器上传图片需要配置js,后台代码
富文本编辑器上传图片需要配置js,后台代码
- JS 上传图片 + 预览功能(一)
JS 上传图片 + 预览功能 <body> <input type="file" id="fileimg1" style="disp ...
- js上传图片前预览方法(支持预览多个图片)
运用js实现上传图片前的预览(支持多张图片),实现的例子如下: 1.源码例子: 1)Js脚本页面 <!doctype html> <html> <head> < ...
- Node.js 上传图片并保存
Node.js 上传图片并保存 依赖 package.json 文件 { "name": "demo", "version": " ...
- js 上传图片、压缩、旋转
亲测 <!doctype html> <html> <head> <meta charset="utf-8"> <title& ...
- 原生 js 上传图片
js <!doctype html> <html> <head> <meta charset="utf-8"> <title& ...
- Js上传图片并生成缩略图
Js上传图片并显示缩略图的流程为 Js选择文件->Jquery上传图片->服务器接收图片流->存储图片->返回结果到Js端->显示缩略图 本文上传图片所用的Js库是aja ...
- 原生js上传图片遇到的坑(axios封装)
后台给我写了一个上传图片的接口,自己用form表单测试成功 接口可以正常跳转 测试的代码: <!doctype html> <html lang="en"> ...
随机推荐
- google学习
https://developers.google.com/machine-learning/crash-course/ https://developers.google.com/machine-l ...
- 为什么说对象字面量赋值比new Object()高效?
http://www.cnblogs.com/mushishi/p/5811743.html
- Docker解决没有vi、vim等命令
1.apt update 同步 /etc/apt/sources.list 和 /etc/apt/sources.list.d 中列出的源的索引,获取到最新的软件包. 2.apt install vi ...
- iOS设计模式 - 装饰
iOS设计模式 - 装饰 原理图 说明 1. cocoa框架本身实现了装饰模式(category的方式实现了装饰模式) 2. 装饰模式指的是动态的给一个对象添加一些额外的职责,相对于继承子类来说,装饰 ...
- 使用HVTableView动态展开tableView中的cell
使用HVTableView动态展开tableView中的cell 效果: 源码: HVTableView.h 与 HVTableView.m // // HVTableView.h // HRVTab ...
- Java学习---InetAddress类的学习
基础知识 1.InetAddress类 在网络API套接字,InetAddress类和它的子类型对象使用域名DNS系统,处理主机名到主机IPv4或IPv6地址的转换.如图1-1所示. 由于InetAd ...
- Linux crontab命令详解与实例
内容有重复的,不过本着宁多勿少的原则就都看看吧,就当加深印象啦 基本格式 :* * * * * command分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用*或者 */1表示第2列表示小时 ...
- ZT 设计模式六大原则(2):里氏替换原则
设计模式六大原则(2):里氏替换原则 分类: 设计模式 2012-02-22 08:46 23330人阅读 评论(41) 收藏 举报 设计模式class扩展string编程2010 肯定有不少人跟我刚 ...
- shell编程技巧和陷阱
先推荐两本经典书籍: 1.advanced bash scripting guide http://www.tldp.org/LDP/abs/abs-guide.pdf 2.Unix Power To ...
- JavaScript浏览器检测之navigator 对象
一.使用客户端检测的原因 由于每个浏览器都具有自己独到的扩展,所以在开发阶段来判断浏览器是一个非常重要的步骤. 虽然浏览器开发商在公共接口方面投入了很多精力,努力的去支持最常用的公共功能: 但在现实中 ...