效果图镇楼。。
 
写的有点乱。上传一个实例供大家参考--附件下载地址如何下:
https://files.cnblogs.com/files/fchx91/uploadFiles.rar
2019-04-08 补充
之前给后台传输的数据格式不太正确。只能拿到给定的一些  name,size,url 这些值。后台需要直接读取files 。
解决思路是:每一次的 appPic 成功后都会 clone 出一个新的 Input[type=file] ,用来存放 file 相关值。
 
HTML--
新增用来存放图片对应的 Input [type=file] ---只用来存值,不用可见。
<div id="picInputDiv" style="display: none;"></div>
JS----
$("#addpic).change()  中增加:
var newInput = $(this).clone();
newInput.attr("id","newInput"+(picFileLen-1)); // picFileLen 是个变量。所以每次拿到的 input id 唯一,避免冲突
$("#picInputDiv").append(newInput);
button.delete--- 
删除一个图片时,$("#newInput"+parIndex) 也要删除。并且,$("#newInput"+parIndex) 只有的兄弟节点,id 属性都要-1
// picInputDiv index>picIndex 角标依次 -1;
var inputLen = $("#picInputDiv").children().length;
var Dvalue = inputLen-1-parIndex;
if(Dvalue ==0) {
$("#picInputDiv").find("#newInput"+parIndex).remove();
} else if(Dvalue > 0) {
$("#picInputDiv").find("#newInput"+parIndex).remove();
for(var i=1; i<Dvalue+1; i++){
$("#newInput"+(parIndex+i)).attr("id","newInput"+(parIndex+i-1));
}
}
 
 
上传附件是工作中经常用到的。一般会直接考虑 input type=file
定义文件选择字段和“浏览。。。”按钮,供文件上传。
 
弊端--
  1. 样式不OK,
  2. 只能显示附件名称
  3. 不能显示附件缩略图
  4. 只能保留最后一次的上传结果,不能实现多个上传
针对以上问题。本来打算使用 dropzone.js。 奈何在插件使用中遇到诸多问题,最后索性自己写了  -_-|||   --【dropzone.js 还需要后续好好研究下】
 
结构--
点击区域,实现 file 上传功能
<div class="btn-add-div">
    <button class="btn-add" data-type="pic"></button>  // file 的外套
    <input type="file" id="addPic" class="btn-add-file" />  // input[type=file] 功能继续保持。但是 opacity=0; 隐藏不可见
</div>
图片信息展示区
<div id="picShowDiv"></div>
 
js--
file  可以给我们提供什么呢???
每次点击 file 传入的内容都不同,想要展示每一个附件相关信息??
 
click  function-->
var picFile = []; //用来存放每一次的 file; 解决 input 职能留存最后一个 file 的问题
var file = document.getElementById("addPic").files[0];  //file 是一个对象
picFile.push(file);
var iName = document.getElementById("addPic").files[0].name; //很奇特。如果这个地方用 file 来替换 document ;会报错!!
var iSize = Math.round(document.getElementById("addPic").files[0].size / 1024); // 换算成 kb 单位
var sizeInfo;  // 如果大于 1M;则换算为 mb 单位
if (iSize>1024){  
sizeInfo = Math.round(iSize / 1024 )+"Mb";
} else {
sizeInfo = iSize +"Kb";
}
var iUrl = URL.createObjectURL($(this)[0].files[0]);  //将本地附件的链接地址转成实际应用的地址!!
var html ="<div class='pic-thumbnail' data-unique='"+iName+"'><button class='delete-content'></button>";  // 加入删除按钮
html += "<p data-value='picName'><strong>"+iName+"</strong></p>";  // 附件名称
html += "<span data-value='picSize'>"+sizeInfo+"</span>";  // 附件大小
html += "<img data-value='picImg' width='130px' src='"+iUrl+"' alt='"+iName+"' />";  //缩略图--遗憾的是,没有点击预览的效果;lightgallery 的融合有问题--后续研究的方向!
$("#picShowDiv").append(html);  //追加到 picShowDiv中,依次展示
 
button.delete-content  -->
点击删除按钮,需要找到 parent().index() 角标。
picFile.splice( index, 1 );
 
优化-->
针对上传有上限的情况
// 监测是否超出限制
function checkLen(len,max){
if(len<max){
return true;
} else {
layer.msg("不能再上传了。已经满额 ");
return false;
}
}
 
CSS--
/*
点击上传附件
*/
.btn-add-div {
width: 150px;
height: 150px;
position: relative;
margin-right: 10px;
display: inline-block;
}
#picShowDiv {
display: inline-block;
}
.btn-add {
position: absolute;
display: block;
width: 100%;
height: 100%;
opacity: 1;
border: 1px dashed #ccc;
border-radius: 5px;
background: #fff;
outline: none;
}
.btn-add:before {
content: '';
position: absolute;
width: 32px;
height: 2px;
left: 50%;
top: 50%;
margin-left: -16px;
margin-top: -1px;
background: #aaa;
}
.btn-add:after {
content: '';
position: absolute;
width: 2px;
height: 32px;
left: 50%;
top: 50%;
margin-left: -1px;
margin-top: -16px;
background: #aaa;
}
.btn-add-file {
position: absolute;
display: block;
width: 100% !important;
height: 100%;
opacity: 0;
}
.btn-add-div:hover {
cursor: pointer;
}
/* 缩略图 */
.pic-thumbnail {
width: 150px;
height: 150px;
box-sizing: border-box;
padding: 10px;
overflow: hidden;
display: inline-block;
border: 1px dashed #ccc;
background: #fff;
position: relative;
border-radius: 5px;
margin-right: 10px;
}
/* 删除 */
.delete-content {
display: inline-block;
position: absolute;
right: 10px;
top: 10px;
width: 20px;
height:20px;
border-radius: 50%;
background: #555;
border: none;
z-index: 10;
}
.delete-content:before {
display: inline-block;
content: '';
position: absolute;
width: 8px;
height: 8px;
left: 6px;
top: 2px;
border-width: 0 2px 2px 0;
border-style: solid;
border-color: #fff;
transform: rotate(45deg);
}
.delete-content:after {
display: inline-block;
content: '';
position: absolute;
width: 8px;
height: 8px;
left: 6px;
top: 10px;
border-width: 0 2px 2px 0;
border-style: solid;
border-color: #fff;
transform: rotate(-135deg);
}
.delete-content:hover {
cursor: pointer;
opacity: .7;
}
 
 
 

file 自定义上传附件并展示缩略图的更多相关文章

  1. Dynamics CRM 自定义上传附件的图片悬浮层显示

    CRM中的附件是以流的形式保存在了数据库中,这样做的一个坏处是一旦系统运行时间久,附件上传的多了势必会导致数据库极速扩大,即影响系统的运行效率也对后期的迁移维护带来了不必要的麻烦.所以很多的客户都会要 ...

  2. salesforce零基础学习(八十九)使用 input type=file 以及RemoteAction方式上传附件

    在classic环境中,salesforce提供了<apex:inputFile>标签用来实现附件的上传以及内容获取.salesforce 零基础学习(二十四)解析csv格式内容中有类似的 ...

  3. java上传附件,批量下载附件(一)

    上传附件代码:借助commons-fileupload-1.2.jar package com.str; import java.io.BufferedInputStream;import java. ...

  4. AntD框架的upload组件上传图片时使用customRequest方法自定义上传行为

    本次做后台管理系统,采用的是 AntD 框架.涉及到图片的上传,用的是AntD的 upload 组件. 我在上一篇文章<AntD框架的upload组件上传图片时使用customRequest方法 ...

  5. React项目中使用wangeditor以及扩展上传附件菜单

    在最近的工作中需要用到富文本编辑器,结合项目的UI样式以及业务需求,选择了wangEditor.另外在使用的过程中发现wangEditor只有上传图片和视频的功能,没有上传文本附件的功能,所以需要对其 ...

  6. jquery 通过ajax FormData 对象上传附件

    之前上传附件都是用插件,或者用form表单体检(这个是很久以前的方式了),今天突发奇想,自己来实现附件上传,具体实现如下 html: <div>   流程图: <input id=& ...

  7. Discuz模拟批量上传附件发帖

    简介 对于很多用discuz做资源下载站来说,一个个上传附件,发帖是很繁琐的过程.如果需要批量上传附件发帖,就需要去模拟discuz 上传附件的流程. 模拟上传 discuz 附件逻辑 dz附件储存在 ...

  8. input file 图片上传

    使用第三方:jquery.ajaxfileupload.jsinput中的name根据后端来定 <form method="post" enctype="multi ...

  9. Dynamic CRM 2013学习笔记(十三)附件上传 / 上传附件

    上传附件可能是CRM里比较常用的一个需求了,本文将介绍如何在CRM里实现附件的上传.显示及下载.包括以下几个步骤: 附件上传的web页面 附件显示及下载的附件实体 调用上传web页面的JS文件 实体上 ...

随机推荐

  1. 使用pyton在本地指定目录模拟服务器

    1.cd 到指定目录 2.运行命令 python 3之前 python -m SimpleHTTPServer & python 3+ python -m http.server & ...

  2. pipe size设置

    我所用的软件架构,使用pipe来实现线程之间的大量数据的传输.在实际操作中,pipe中传输的是数据的指针,而不是数据本身.  但是在调试过程中,我发现,如果我尝试往pipe里面write10000个指 ...

  3. logback日志框架的简单使用

    1.首先在maven中增加依赖 <dependency> <groupId>ch.qos.logback</groupId> <artifactId>l ...

  4. 阿狸V任务页面爬取数据解析

    需求: 爬取:https://v.taobao.com/v/content/video 所有主播详情页信息 首页分析 分析可以得知数据是通过ajax请求获取的. 分析请求头 详情页分析 详情页和详情页 ...

  5. ctrl+shift+r / ctrl+f5 强制(不使用缓存)刷新google chrome网页

    我改了csdn图片后, 一直看到的是旧图片, n天之后, 还是旧图片.猜测应该是用了缓存(且缓存更新逻辑失败, 定是csdn的bug), 用ctrl+shift+r, 或者ctrl+f5, 强制刷新页 ...

  6. BAT面试题:请使用递归构建N叉树

    题目要求: 现在我们拥有全国的省.市.县.镇的行政信息,比如 浙江省 -> 杭州市 -> 西湖区 --> xx街道,请将这些信息构建成一棵树,根节点为全国,叶子节点为镇. 我的误解: ...

  7. IDEA快捷建使用

    因为以前使用的IDE工具是Eclipse,所以在公司中都是用IDEA的情况下,换成了IDEA,感觉是好用了很多. 其他快捷键: Ctrl+Shift + Enter,语句完成 “!”,否定完成,输入表 ...

  8. windows安全更新程序(KB4093112) 安装失败 错误0x80070011

    解决办法:win + R → 输入regedi 将默认安装路径改回C盘的program files

  9. 以API方式调用C# dll,使用OneNote2013 sp1实现OCR识别本地图片

    http://www.cnblogs.com/Charltsing/p/OneNoteOCRAPI.html OneNote2013 OCR API调用使用说明2019.4.17 使用说明:1.安装干 ...

  10. 我的工具:Ping工具

    C# Ping工具 通过该工具可以多个地点Ping服务器以检测服务器响应速度,同时也可以测试网站的响应速度,解析时间,服务器连接时间,下载速度 工具下载地址:https://download.csdn ...