CI框架结合jQuery实现上传多张图片即时显示
一、Html代码如下:
<tr>
<td class="txt_r"><span class="orange">* </span>上传图片:</td>
<td id="picInput">
<form id="upload_form" method="post" enctype="multipart/form-data" style="" action="/admin/upload/uploadCover" >
<input type="file" size="30" name="userfile[]" id="userfile" class="input" onchange="$('#upload_form').submit();return false;" multiple="multiple">
</form>
</td>
</tr>
<tr>
<td>
</td>
<td>
<div id="preview_cover" style="display:none;margin-top: 5px;" >
</div>
</td>
</tr>
二、jQuery代码如下:
$(function(){
$('#upload_form').submit(function(){
var val = $("#userfile").val();
if(val === ''){
weebox_popInfo('e','请选择要上传的图片!','','alert');
return false;
}
var filepath =/\.(jpg|jpeg|gif|JPG|JPEG|GIF|PNG|png)$/i;
if(!filepath.test(val)){
weebox_popInfo('e','要求为图片文件!','','alert');
return false;
}
$(this).ajaxSubmit(uploadOptions);
return false;
});
var uploadOptions = {
dataType:'json',
clearForm:true,
resetForm:true,
beforeSubmit: beforeSubmit,
success: function(json) {
var imgStr = "";
var urlString = "";
if(json.code==1){
var imgArr = json.url;
if($.isArray(imgArr)){
urlString = imgArr.join(',');
for(var i=0;i<imgArr.length;i++){
imgStr += "<div id='thumb"+i+"' style='width:100px; height:125px; float:left;margin-right: 10px;'>";
imgStr += "<img src='"+imgArr[i]+"' style='width: 100px;height: 100px;float: left;'/><br/>";
imgStr += "<input type='radio' name='default_picture' id='default_picture' value='"+imgArr[i]+"' style='width: 20px;height: 18px;float:left;' />主图";
imgStr += "<a id='delete' onclick='delete_cover(this)' style='width: 20px;height: 20px;margin-left: 12px;text-decoration: none;color:#ff5a54;'>删除</a>";
imgStr += "</div>";
}
}else{
urlString = imgArr;
imgStr += "<div id='thumb0' style='width:100px; height:125px; float:left;margin-right: 10px;'>";
imgStr += "<img src='"+imgArr+"' style='width: 100px;height: 100px;float: left;'/><br/>";
imgStr += "<input type='radio' name='default_picture' id='default_picture' value='"+imgArr+"' style='width: 20px;height: 18px;float:left;' />主图";
imgStr += "<a id='delete' onclick='delete_cover(this)' style='width: 20px;height: 20px;margin-left: 12px;text-decoration: none;color:#ff5a54;'>删除</a>";
imgStr += "</div>";
}
var existUrl = $("#pictures").val();
var inputTab = "";
if(typeof existUrl !== "undefined"){
var existArr = existUrl.split(',');
for(var j=0;j<imgArr.length;j++){
existArr.push(imgArr[j]);
}
var changeUrl = existArr.join(',');
$("#pictures").val(changeUrl);
}else{
inputTab = "<input type='hidden' name='pictures' id='pictures' value='"+urlString+"'>";
}
imgStr += inputTab;
$('#preview_cover').append(imgStr).show();
}else{
weebox_popInfo('e',json.msg,'','alert');
}
}
};
var beforeSubmit = function() {}
})
function delete_cover(tag) {
if($(tag).prev().attr("checked")){
weebox_popInfo('i', "不能删除主图!");
}else{
$(tag).parent().remove();
}
var checked_img = $(tag).prev().val();
var pictures = $('#pictures').val();
var pictureArr = pictures.split(',');
for(var i=0;i<pictureArr.length;i++){
if(checked_img == pictureArr[i]){
pictureArr.splice(i,1);
}
}
$("#pictures").val(pictureArr);
}
三、PHP代码如下:
public function uploadCover()
{
$dirpath='./upload/profile/';
if(!is_dir($dirpath)){
mkdir($dirpath);
}
$config['upload_path'] = $dirpath;
$config['allowed_types'] = 'jpg|jpeg|gif|JPG|JPEG|GIF|PNG|png';
$config['encrypt_name']=true;
$this->load->library('upload', $config);
$count=count($_FILES["userfile"]["name"]);//页面取的默认名称
$url_arr=array();
for($i=0;$i<$count;$i++){
$field_name = 'cover' . '_' . $i;
$_FILES[$field_name] = array('name' => $_FILES["userfile"]['name'][$i],
'size' => $_FILES["userfile"]['size'][$i],
'type' => $_FILES["userfile"]['type'][$i],
'tmp_name' => $_FILES["userfile"]['tmp_name'][$i],
'error' => $_FILES["userfile"]['error'][$i] );
if ($this->upload->do_upload($field_name)) { //默认名是:userfile
$upload_data = $this->upload->data();
$image_url = array();
$image_url['image_url'] = '/misc/upload/profile/'.$upload_data['file_name'];
$url_arr[] = $image_url;
}else{
echo $this->upload->display_errors();
exit(json_encode(array('code'=>0,'msg'=>'系统错误!')));
}
}
$url_arr = array_column($url_arr,'image_url');
exit(json_encode(array("code"=>1,"url"=>$url_arr))); }
}
四、效果图如下:

CI框架结合jQuery实现上传多张图片即时显示的更多相关文章
- CI框架整合UEditor编辑器上传功能
最近项目中要使用到富文本编辑器,选用了功能强大的UEditor,接下来就来讲讲UEditor编辑器的上传功能整合. 本文UEditor版本:ueditor1_4_3_utf8_php版本 第一步:部署 ...
- JS框架_(JQuery.js)上传进度条
百度云盘 传送门 密码: 1pou 纯CSS上传进度条效果: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN ...
- 项目一:第四天 1、快递员的条件分页查询-noSession,条件查询 2、快递员删除(逻辑删除) 3、基于Apache POI实现批量导入区域数据 a)Jquery OCUpload上传文件插件使用 b)Apache POI读取excel文件数据
1. 快递员的条件分页查询-noSession,条件查询 2. 快递员删除(逻辑删除) 3. 基于Apache POI实现批量导入区域数据 a) Jquery OCUpload上传文件插件使用 b) ...
- IE8/9 JQuery.Ajax 上传文件无效
IE8/9 JQuery.Ajax 上传文件有两个限制: 使用 JQuery.Ajax 无法上传文件(因为无法使用 FormData,FormData 是 HTML5 的一个特性,IE8/9 不支持) ...
- 从零开始编写自己的C#框架(23)——上传组件使用说明
文章导航 1.前言 2.上传组件功能说明 3.数据库结构 4.上传配置管理 5.上传组件所使用到的类 6.上传组件调用方法 7.效果演示 8.小结 1.前言 本系列所使用的是上传组件是大神July开发 ...
- 强大的支持多文件上传的jQuery文件上传插件Uploadify
支持多文件上传的jQuery文件上传插件Uploadify,目前此插件有两种版本即Flash版本和HTML5版本,对于HTML5版本会比较好的支持手机浏览器,避免苹果手机Safari浏览器不支持Fla ...
- jQuery文件上传插件Uploadify(转)
一款基于flash的文件上传,有进度条和支持大文件上传,且可以多文件上传队列. 这款在flash的基础上增加了html5的支持,所以在移动端也可以使用. 由于官方提供的版本是flash免费,html5 ...
- jquery.uploadify上传文件配置详解(asp.net mvc)
页面源码: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...
- jQuery文件上传插件jQuery Upload File 有上传进度条
jQuery文件上传插件jQuery Upload File 有上传进度条 jQuery文件上传插件jQuery Upload File,插件使用简单,支持单文件和多文件上传,支持文件拖拽上传,有进度 ...
随机推荐
- pg数据库中时间查询的方式
方法一:select * from user_info where create_date>= '2015-07-01' and create_date < '2015-08-15'; 方 ...
- spring mvc中添加对Thymeleaf的支持
一.下载Thymeleaf 官方下载地址:https://dl.bintray.com/thymeleaf/downloads/thymeleaf/ 我下载的是最新的3.0.11版本 把包里的jar包 ...
- 在 Alpine Linux 中安装 bash shell
如何在我的 Alpine Linux LXD(Linux容器)虚拟机(VM)中安装 bash shell? Alpine Linux 自带 BusyBox 工具.它被评为“嵌入式 Linux 的瑞士军 ...
- NPU TPU
https://github.com/tensorflow/tpu/tree/master/models/official/efficientnet
- Request header field * is not allowed by Access-Control-Allow-Headers in preflight response问题解决
跨域问题报错信息为:Failed to load http://192.168.30.119: Request header field language is not allowed by Acce ...
- MongoDB开发深入之二:索引
索引分类: 默认索引 单一索引 复合索引 多键索引(数组索引) 全文检索索引 2dsphere 索引 2D索引 ...... 索引属性: 到期TTL 唯一索引 部分索引 稀疏索引 索引通常能够极大的提 ...
- Laya一个莫名其妙的报错laya.pathfinding
版本2.2.0 莫名其妙,然后论坛查了下 把类库设置,不勾选这玩意就不报错了.
- IntelliJ IDEA Error:(24, 35) java: 常量字符串过长
在转换一个JSON转Java对象是 idea 编译不通过 提示:Error:(24, 35) java: 常量字符串过长 File -> Settings -> Build,Execut ...
- nexus 3.x最新版下载安装和上传下载jar
注意: nexus 3.x最新版好像不用下载索引了,目前我使用一些基本功能没有索引也能耍的很6 下载 nexus最新版下载https://www.sonatype.com/download-oss-s ...
- CSS布局:sticky定位
stick定位一如其名:它随“正常”文档流而动,直到规定位置,尔后“粘”在那里:或者,当它发现自己可以跟随“正常”文档流而脱离sticky位置时,就果断离开从而加入文档流. 代码与效果如下: < ...