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,插件使用简单,支持单文件和多文件上传,支持文件拖拽上传,有进度 ...
随机推荐
- PHP系列 | [转] PHP中被忽略的性能优化利器:生成器
官方:https://www.php.net/manual/zh/language.generators.overview.php 原文:https://segmentfault.com/a/1190 ...
- MLflow系列1:MLflow入门教程(Python)
英文链接:https://mlflow.org/docs/latest/tutorial.html 本文链接:https://www.cnblogs.com/CheeseZH/p/11943280.h ...
- 使用Alibaba的Nacos做为SpringCloud的注册和配置中心,并结合Sentinel+Nocos动态进行限流熔断
最近在学习阿里的Nacos组件以及Sentinel组件,折腾出了一个小demo. Git地址:https://github.com/yangzhilong/nacos-client 有兴趣的小伙伴可以 ...
- c++ extra qualification
原 c++ extra qualification 2013年01月15日 10:04:52 沈纵情 阅读数 9728 运行代码时候遇到了如下错误: extra qualification ‘Co ...
- CMDB资产采集的四种方式
转 https://www.cnblogs.com/guotianbao/p/7703921.html 资产采集的概念 资产采集的四种方式:Agent.SSH.saltstack.puppet 资产采 ...
- Asp Core部署到IIS服务器
之前有文章写了.Asp Core Kestrel服务器可以独立运行在linux下,也可以部署到Docker上面通过容器管理,当然也可以直接部署到IIS中 一:安装环境 1)首先需要在服务器安装对应环境 ...
- [IOT] - 使用 .Net Core 操作 GPIO 引脚点亮 LED 灯泡
1. 在 VS 2019 中创建 .Net Core 控制台应用程序,使用 Nuget 安装程序包: System.Device.GpioIot.Device.Bindings 2. 更新 Main ...
- 218. The Skyline Problem (LeetCode)
天际线问题,参考自: 百草园 天际线为当前线段的最高高度,所以用最大堆处理,当遍历到线段右端点时需要删除该线段的高度,priority_queue不提供删除的操作,要用unordered_map来标记 ...
- 虚拟机出现“The system is running in low-graphics mode”的解决方法
我用虚拟机安装了ubuntu的版本后,又在上面安装了几个插件,第二次打开虚拟机就出现下面的提示: 通过在网上查找教程,找到了以下的解决方法 1.Ctrl+Alt+F1进入控制台 2.输入用户密码 3. ...
- 【Python爬虫案例学习】python爬取淘宝里的手机报价并以价格排序
第一步: 先分析这个url,"?"后面的都是它的关键字,requests中get函数的关键字的参数是params,post函数的关键字参数是data, 关键字用字典的形式传进去,这 ...