* 上传图片:

* 上传图片:
 首页 > 作文

CI框架结合jQuery实现上传多张图片即时显示

更新时间:2023-04-07 06:36:55 阅读: 评论:0

一、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="urfile[]" id="urfile" class="input" onchange="$('#upload_form').submit();return fal;" 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 = $("#urfile").val();

if(val === ''){
weebox_popinfo('e','请选择要上传的图片!','','alert');
return fal;
}
var filepath =/\.(jpg|jpeg|gif|jpg|jpeg|gif|png|png)$/i;
if(!filepath.test(val)){
weebox_popinfo('e','要求为图片文件!','','alert');
return fal;
}
$(this).ajaxsubmit(uploadoptions);
return fal;
});

var uploadoptions = {
datatype:'json',
clearform:true,
retform: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>";
}
}el{
urlstring = imgarr;
imgstr += "<dcos90°iv 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);
}el{
inputtab = "<input type='hidden' name='pictures' id='pictures' value='"+urlstring+"'>";
}
imgstr += inputtab;
$('#preview_cover').append(imgstr).show();
}el{
weebox_popinfo('e',json.msg,'','alert');
}
}

};

var beforesubmit = function() {}
})


function delete_cover(tag) {

if($(tag).prev().attr("checked")){
weebox_popinfo('i', "不能删除主图!");
}el{
$(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 func实践过程tion 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["urfile"]["name"]);//页面取的默认名称
$url_arr=array();
for($i=0;$i<$count;$i++){
$field_name = 'c二会什么时候结束over' . '_' . $i;
$_files[$field_name] = array('name' => $_files["urfile"]['name'][$i],
'size' => $_files["urfile"]['size'][$i],
'type' => $_files["urfile"河南话]['type'][$i],
'tmp_name' => $_files["urfile"]['tmp_name'][$i],
'error' => $_files["urfile"]['error'][$i] );
if ($this->upload->do_upload($field_name)) { //默认名是:urfile
$upload_data = $this->upload->data();
$image_url = array();
$image_url['image_url'] = '/misc/upload/profile/'.$upload_data['file_name'];
$url_arr[] = $image_url;
}el{
echo $this->upload->display_errors();
exipurple是什么颜色t(json_encode(array('code'=>0,'msg'=>'系统错误!')));
}
}
$url_arr = array_column($url_arr,'image_url');
exit(json_encode(array("code"=>1,"url"=>$url_arr))); }
}

四、效果图如下:

本文发布于:2023-04-07 06:36:53,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/cb7ea0a6df6aac8980fbc8b554117263.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:CI框架结合jQuery实现上传多张图片即时显示.doc

本文 PDF 下载地址:CI框架结合jQuery实现上传多张图片即时显示.pdf

标签:代码   什么时候   图片   河南
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图