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上传图片的代码,记录一下的更多相关文章

  1. c#使用js上传图片

    前几天朋友说用js上传图片过去遇到点问题,于是自己也想写一个demo这里就把自己挖的坑填了. 话不多说上代码 前台就一个file控件加按钮 <!DOCTYPE html> <html ...

  2. 富文本编辑器上传图片需要配置js,后台代码

    富文本编辑器上传图片需要配置js,后台代码

  3. JS 上传图片 + 预览功能(一)

    JS 上传图片 + 预览功能 <body> <input type="file" id="fileimg1" style="disp ...

  4. js上传图片前预览方法(支持预览多个图片)

    运用js实现上传图片前的预览(支持多张图片),实现的例子如下: 1.源码例子: 1)Js脚本页面 <!doctype html> <html> <head> < ...

  5. Node.js 上传图片并保存

    Node.js 上传图片并保存 依赖 package.json 文件 { "name": "demo", "version": " ...

  6. js 上传图片、压缩、旋转

    亲测 <!doctype html> <html> <head> <meta charset="utf-8"> <title& ...

  7. 原生 js 上传图片

    js <!doctype html> <html> <head> <meta charset="utf-8"> <title& ...

  8. Js上传图片并生成缩略图

    Js上传图片并显示缩略图的流程为 Js选择文件->Jquery上传图片->服务器接收图片流->存储图片->返回结果到Js端->显示缩略图 本文上传图片所用的Js库是aja ...

  9. 原生js上传图片遇到的坑(axios封装)

    后台给我写了一个上传图片的接口,自己用form表单测试成功 接口可以正常跳转 测试的代码: <!doctype html> <html lang="en"> ...

随机推荐

  1. Spring MVC基本配置和实践(二)

    1. springmvc: 是一个表现层框架,作用是从请求中接收传入的参数,将处理后的结果数据返回给页面展示 2. ssm整合: 1)Dao层 pojo.mapper接口.mapper映射文件(使用逆 ...

  2. HTML5盒子模型。

    盒子模型. 盒子由 margin.border.padding.content 四部分组成. margin :外边距 border:边框 padding:内边距 (内容与边框的距离) content: ...

  3. In-app Billing 概述

    这个文档讲解了In-app Billing的基础构成和功能,为了能够把In-app Billing功能添加到你的程序中,你需要很好的理解这些内容. 注意: 保证你的应用遵从发布地国家的相关法律. 举例 ...

  4. windows 下 gdb 的安装

    在 windows 下 gcc/g++ 的安装 这篇文章中已经提到,用MinGW Installation Manager可以方便地管理 MinGW 组件,因此使用该软件安装 gdb . 打开 Min ...

  5. [BZOJ 1588][HNOI 2002] 营业额统计

    这果然是在那个没有STL的年代出的题 1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 16648  Solve ...

  6. Mac appium iOS 安装命令

    1. brew install node 2. npm install -g appium #或者用安装包安装 3. brew install libimobiledevice --HEAD # in ...

  7. Linux命令--用户管理

    useradd命令 Linux useradd命令用于建立用户帐号. useradd可用来建立用户帐号.帐号建好之后,再用passwd设定帐号的密码.而可用userdel删除帐号.使用useradd指 ...

  8. ZooKeeper教程(一)----Centos7下安装ZooKeeper(单机版)

    1.下载源码 官网下载地址: http://mirrors.hust.edu.cn/apache/zookeeper/ 选择最新的版本进行下载 这里选择3.4.10进行下载: wget http:// ...

  9. CString char BSTR 转换

     关于字符集不一的历史原因,可以参考: UNICODE与ANSI的区别 以下是网上转载的资料.我将辅以自己的实例,说明并总结关系. 一.CString, int, string, char*之间的转换 ...

  10. A blog about Core Animation and other iOS graphics framework

    A blog about Core Animation and other iOS graphics frameworks. https://www.calayer.com/