批量预览本地图片

复制代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>get file input full path</title>
<style>
.case_photo_area_small {
    width: 770px;
        overflow:hidden;
}

.small_photo {
    border-top: 2px #BFBFBF solid;
    border-right: 2px #BFBFBF solid;
    border-left: 2px #BFBFBF solid;
    border-bottom: 2px #BFBFBF solid;
}

.small_photo_selected {
    border-top: 2px #FFCC00 solid;
    border-right: 2px #FFCC00 solid;
    border-left: 2px #FFCC00 solid;
    border-bottom: 2px #FFCC00 solid;
}
</style>
<script type="text/javascript" language="javascript"
    src="native_picture.js"></script>
</head>
<body>
    <table cellpadding="0" cellspacing="0">
        <tbody id="_container">
            <tr>
                <td><input type="file" id="FileUpload1" name="FileUploads"
                    onchange="getFullPath(this,'FileUpload1',true);" /> <a
                    href="javascript:void(0);" onclick="addFile()">添加</a></td>
            </tr>
        </tbody>
    </table>
    <table border=0 cellspacing=0 cellpadding=0 style="display: none;"
        id="tableImg">
        <tr>
            <td><img id="beforeX" src="images/left.png"></td>
            <td>
                <div class="case_photo_area_small" id="showareX">
                    <table border=0 cellspacing="1px" cellpadding=0>
                        <tr id="_newPreview"></tr>
                    </table>
                </div>
            </td>
            <td><img id="nextX" src="images/right.png"></td>
            <td><a href="javascript:void(0);" id="deleThisImg">【删除图片】 </a></td>
        </tr>
    </table>
</body>
</html>
复制代码
?
//native_picture.js
var id = 0;
function addFile() {
     var tab = document.getElementById( "_container" );
     var row = document.createElement( "tr" );
     var cell = document.createElement( "td" );
     var f = document.createElement( "input" );
     f.setAttribute( "type" , "file" );
     f.setAttribute( "id" , "FileUpload" + (id + 2));
     f.setAttribute( "name" , "FileUploads" );
     f.setAttribute( "contentEditable" , false );
     cell.appendChild(f);
 
     var btndele = document.createElement( "a" );
     btndele.setAttribute( "href" , "javascript:void(0)" );
     btndele.setAttribute( "class" , "btn2" );
     btndele.innerHTML = "<span class='toolbar_icon toolbar_icon_07'></span>删除" ;
     btndele.setAttribute( "value" , "删除" );
     btndele.setAttribute( "Id" , "del" + id);
     btndele.onclick = function () {
         // 删除行
         getFullPath( this .parentNode.childNodes[0],
                 this .parentNode.childNodes[0].getAttribute( "id" ), false );
         while ( true ) {
             if (btndele.tagName == "TD" ) {
                 break ;
             } else {
                 btndele = btndele.parentNode;
                 btndele.parentNode.removeChild(btndele);
             }
         }
 
     };
     f.onchange = function () {
         getFullPath( this , this .getAttribute( "id" ), true );
     };
     cell.appendChild(btndele);
     row.appendChild(cell);
     tab.appendChild(row);
 
     id++;
 
}
var goIndex = 0;
var countx = 0;
function getFullPath(obj, fileId, falg) {
     //obj:file对象;fileId:"FileUpload"+id; falg:ture 添加或更新图片,false 删除图片
     
     var gindex = fileId.substring(10); //取得与file之对应图片id号
     var divImg = document.getElementById( "divImg" + gindex);   // 判断是否含有此id图片
     var newPreview = document.getElementById( "_newPreview" );  //指定TR添加节点
     var newPreviewSize = newPreview.childNodes.length;  //TR已添加TD数
     var index =0;     //获得图片索引
     if (newPreviewSize>0){
         index=getIndex();
     }
 
     
     var patn = /.jpg$|.jpeg$|.bmp$/i;   //正则表达式判断上传文件
     countx = newPreviewSize;
     
     if (patn.test(obj.value)) {
         if (falg && divImg == null ) {
             var cell = document.createElement( "td" );
             var g = document.createElement( "div" );
             g.setAttribute( "id" , "divImg" + gindex);
             g.style.width = "90px" ;
             g.style.height = "90px" ;
             //指定位置添加图片
             if (!newPreview.hasChildNodes()||newPreviewSize==index) {
                 cell.appendChild(g);
                 newPreview.appendChild(cell);
                 goIndex=newPreviewSize-1;
             } else {
                     cell.appendChild(g);
                     newPreview.insertBefore(cell, newPreview.childNodes[index]);
                     goIndex=index-1;
             }
             iephoto(g);
             countx++;
             nextX();
         } else if (falg) {
             iephoto(divImg);
             if (newPreviewSize==1){
                 goIndex=0;
             } else {
                 goIndex=index+1;
             }
             beforeX();
         } else {
             delephoto();
         }
 
     } else if (divImg != null ) {
         delephoto();
     } else {
         return false ;
     }
     styleDivImg();
     showhidden();
     
     document.getElementById( "beforeX" ).onclick= function (){
         beforeX();
         styleDivImg();
     };
     document.getElementById( "nextX" ).onclick= function (){
         nextX();
         styleDivImg();
     };
     
     // 删除选中的图片
      document.getElementById( "deleThisImg" ).onclick= function (){
             var imgNow =newPreview.childNodes[goIndex].getElementsByTagName( "div" )[0];
             var  buttonIndex=parseInt(imgNow.getAttribute( "id" ).substring(6));
             var buttonNow =document.getElementById( "FileUpload" +buttonIndex);
          if (imgNow&&buttonNow){
              imgNow.parentNode.parentNode.removeChild(imgNow.parentNode);
              //删除button,若为第一个就清空值
              if (buttonIndex==1){
                  //buttonNow.select();  
                  //document.execCommand("delete");
                  buttonNow.outerHTML=buttonNow.outerHTML;
              } else {
                     while ( true ) {
                         if (buttonNow.tagName == "TD" ) {
                         break ;
                         } else {
                             buttonNow =buttonNow.parentNode;
                             buttonNow.parentNode.removeChild(buttonNow);
                       }
                   }
              }
                  countx--;
                  beforeX();
                  styleDivImg();
                  showhidden();
                 
          }
         
      };
//图片样式
function styleDivImg(){
     for ( var j=0;j<countx;j++){
         if (j==goIndex)
             newPreview.childNodes[j].className= "small_photo_selected" ;
         else
             newPreview.childNodes[j].className= "small_photo" ;
     }
}
 
//IE浏览器浏览本地DIV图片路径
function iephoto(ie){
           obj.select();  
         ie.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);" ;
         ie.filters.item( "DXImageTransform.Microsoft.AlphaImageLoader" ).src = document.selection.createRange().text;
}
 
 
//删除图片
function delephoto(){  
     if (newPreviewSize==1){
         goIndex=1;
     } else {
         goIndex= index;
     }
     countx--;
     
     divImg.parentNode.parentNode.removeChild(divImg.parentNode);
     beforeX(); 
}  
//获取索引
function getIndex(){
         var arr = new Array();
         var indexg =0;
           // document.getElementById("divImg" + jj);
        for ( var jj = 0; jj < newPreviewSize; jj++) {
            var gchild = newPreview.childNodes[jj].getElementsByTagName( "div" )[0];   
             arr[jj] = parseInt(gchild.getAttribute( "id" ).substring(6));
         }
         if (divImg== null &&falg){
         arr[newPreviewSize] = gindex;
         }
         //对数字排序
         function sortNumber(a,b)
         {
         return a - b;
         }
         arr = arr.sort(sortNumber);
         
         for ( var jj=0;jj<arr.length;jj++){
             if (arr[jj]==gindex){
                 indexg=jj;
                 break ;
             }
         }
         return indexg;
     }
 
}
//显示隐藏
function showhidden(){
     var tableImg = document.getElementById( "tableImg" );
     if (countx==0){
         tableImg.style.display= "none" ;
     } else {
         tableImg.style.display= "block" ;
     }
}
//上一个
function beforeX() {
     if (goIndex <= 0) {
         goIndex = countx-1;
     } else {
         goIndex--;
     }
     document.getElementById( "showareX" ).scrollLeft = (93) * goIndex;
}
//下一个
function nextX() {
     if (goIndex+1>=countx) {
         goIndex = 0;
     } else {
         goIndex++;
     }
     document.getElementById( "showareX" ).scrollLeft = (93) * goIndex;
}

 

转载于:https://www.cnblogs.com/jpfss/p/7372128.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 layui 实现批量上传图片的代码示例: HTML 代码: ```html <div class="layui-upload"> <button type="button" class="layui-btn" id="test-upload-normal">选择文件</button> <div class="layui-upload-list"> <table class="layui-table"> <thead> <tr> <th>文件名</th> <th>大小</th> <th>状态</th> <th>操作</th> </tr> </thead> <tbody id="demoList"></tbody> </table> </div> <button type="button" class="layui-btn" id="test-upload-normal-submit">上传</button> </div> ``` JavaScript 代码: ```javascript //执行实例 layui.use('upload', function(){ var $ = layui.jquery ,upload = layui.upload; //普通图片上传 var uploadInst = upload.render({ elem: '#test-upload-normal' ,url: '/upload/' ,multiple: true //允许多文件上传 ,before: function(obj){ //读本地文件示例,不支持ie8 obj.preview(function(index, file, result){ $('#demoList').append('<tr><td>'+ file.name +'</td><td>'+ (file.size/1024).toFixed(1) +'kb</td><td>等待上传</td><td><button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button><button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button></td></tr>') }); } ,done: function(res){ //上传完毕 console.log(res); } ,error: function(){ //请求异常回调 console.log('上传失败'); } }); //批量上传图片 $('#test-upload-normal-submit').on('click', function(){ //获取文件列表 var file_list = $('#demoList tr'); //遍历文件列表 file_list.each(function(index, elem){ //获取文件名 var file_name = $(elem).find('td:first-child').text(); //获取文件状态 var file_status = $(elem).find('td:nth-child(3)').text(); //如果状态是等待上传,则开始上传 if(file_status === '等待上传'){ //上传文件 uploadInst.upload(index, $(elem)); } }); }); //监听删除事件 $('#demoList').on('click', '.demo-delete', function(){ $(this).parents('tr').remove(); }); }); ``` 上述代码中,我们使用了 layui 的 upload 模块实现了批量上传图片的功能。用户可以通过点击“选择文件”按钮选择多个图片文件,上传前会图片,然后点击“上传”按钮,上传所有等待上传的图片文件。在上传过程中,用户可以删除某个文件,也可以重传某个文件。上传完成后,会在控制台输出上传结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值