input file相关知识简例

在此介绍的input file相关知识为: 上传照片及文件,其中包括单次上传、批量上传、删除照片、增加照片、读取图片、对上传的图片或文件的判断,比如限制图片的张数、限制图片的格式、大小等。

在开发中,文件上传必不可少,<input type="file" /> 是常用的上传标签,但特别low、浏览的字样又不能换,但难不倒强迫症患者...看一些其他网站有的将<input type="file" />隐藏,用点击其他的标签(图片等)来时实现选择文件上传功能,也有的将其设为透明,这里推荐后者,详情请参考下面代码。

此为本人写的demo,更人性化的效果实现,供分享,不足之处望提议,将不胜感激

 <!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.kuang{
display: inline-block;
vertical-align: middle;
box-shadow: inset 0px 1px 20px 5px #ddd;
text-align: left;
margin-right: 20px;
margin-bottom: 20px;
width: 165px;
height: 165px;
}
.addhao{
width: 165px;
height: 165px;
background: url(../img/add_photo_plus.png);
}
.on{
display: inline-block;
text-align: left;
margin-right: 20px;
margin-bottom: 20px;
width: 165px;
height: 165px;
display: none;
position: relative;
overflow: hidden;
line-height: 200px;
}
.xian{
width: 165px;
height: 165px;
position: absolute;
background-image: linear-gradient(
0deg,
rgba(0,0,0,0.7) 50%,
transparent 50%
);
background-size: 165px 4px;
display: none;
z-index: 2;
}
.chahao{
position: absolute;
width: 60px;
height: 60px;
background: url(../img/ico_02.png);
background-position: -171px -158px;
top: 52.5px;
left: 52.5px;
display: none;
z-index: 2;
}
.on img{
width: 100%;
height: auto;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin:auto;
}
.kuang input{
width: 164px;
height: 164px;
opacity: 0;
}
.button{
width: 500px;
height: 36px;
margin: 0 auto; }
.button .set{
float: left;
width: 216px;
height: 36px;
background: #ddd;
text-align: center;
position: relative;
}
.set input{
width: 216px;
height: 36px;
opacity: 0;
position: absolute;
top: 0;
left: 0;
}
.button .next{
float: right;
width: 216px;
height: 36px;
background: red;
color: white;
}
.bigk{
width: 1000px;
margin: 0 auto;
text-align: center;
}
</style>
<script src="branches/jquery-3.1.1.min.js"></script>
</head>
<body>
<div class="bigk">
<div class="kuang">
<div class="addhao">
<input type="file" name="" class="fileinput">
</div>
<div class="on">
<div class="xian"></div>
<div class="chahao"></div>
</div>
</div>
<div class="kuang">
<div class="addhao">
<input type="file" name="" class="fileinput">
</div>
<div class="on">
<div class="xian"></div>
<div class="chahao"></div>
</div>
</div>
<div class="kuang">
<div class="addhao">
<input type="file" name="" class="fileinput">
</div>
<div class="on">
<div class="xian"></div>
<div class="chahao"></div>
</div>
</div>
<h3>上传n张照片,生成属于你的相册</h3>
<p><span style="color:red;">限制条件:可自行增减设置;</span>如:支持jpg/png/jpeg格式,单张照片不大于20M,单次上传不多于n张,请尽量上传横板照片</p>
<img src="../img/line_03.png" alt="">
<div class="button" >
<a href=""><div class="set">批量上传<input type="file" name="" multiple="multiple" id="piliang"></div></a>
<a href="#" class="next">限制张数</a>
</div>
</div>
</div>
</div>
<script>
// 单张上传照片 删除照片
$(" .fileinput").change(function () {
var file = this.files[0];
readFile(file,$(this).parent().siblings(".on"));
});
$(".on").mouseover(function () {
$(this).children(".xian").show();
$(this).children(".chahao").show(); });
$(".on").mouseleave(function () {
$(this).children(".xian").hide();
$(this).children(".chahao").hide();
});
$(".chahao").click(function () {
$(this).next().remove();
$(".xian").hide();
$(".chahao").hide();
$(this).parent().hide();
$(this).parent().siblings(".addhao").show();
$(this).parent().siblings(".addhao").children().val(""); });
var on =document.querySelector(".on");
// 需要把阅读的文件传进来file element是把读取到的内容放入的容器
function readFile(file,element) {
// 新建阅读器
var reader = new FileReader();
// 根据文件类型选择阅读方式
switch (file.type){
case 'image/jpg':
case 'image/png':
case 'image/jpeg':
case 'image/gif':
reader.readAsDataURL(file);
break;
}
// 当文件阅读结束后执行的方法
reader.addEventListener('load',function () {
// 如果说让读取的文件显示的话 还是需要通过文件的类型创建不同的标签
switch (file.type){
case 'image/jpg':
case 'image/png':
case 'image/jpeg':
case 'image/gif':
var img = document.createElement('img');
img.src = reader.result;
element.append(img);
element.siblings(".addhao").hide();
element.show();
break;
}
});
}
// 批量上传部分
var piliang = document.querySelector('#piliang');
var on = $(".on");
piliang.addEventListener('change',function () {
for (var i = 0;i < this.files.length;i++){
var file = this.files[i];
on.eq(i).children(".chahao").next().remove();
readFile(file,on.eq(i));
}
});
//
var on = $(".on");
$(".next").click(function () {
for (var i = 0; i < 10; i++) {
console.log(on[i].childNodes.length);
if (on[i].childNodes.length==6){
//这个判断就是说明里面有多少个孩子,孩子不够数,不会生成相册
}else{
alert("上传照片不足3张");
$(".next").attr("href","javascript:void(0)");
return
}
$(".next").attr("href","生成相册.html");
}
});
</script>
</body>
</html>

初始效果图如下

点击+号进行添加图片效果图如下

 点击+号添加完成单次上传图片效果图如下

点击批量进行上传效果图如下:

删除上传照片效果图如下

当没有满足js中的设置要求,将不能提交

解析:

选择文件:input:file 选择本地文件,默认为单选,多选为multiple;

读取文件:需要阅读器 新建reader;

有关js这里用的是jq,在用此方法前,请将jq链接到页面;

最新文章

  1. 修复bootstrap daterangepicker中的3个问题
  2. 3 HTML&amp;JS等前端知识系列之javascript的基础
  3. Notification NotificationManager RemoteViews PendingIntent
  4. 解决TextView最后一行显示不全
  5. 卖萌的极致!脸部捕捉软件FaceRig让你化身萌宠
  6. 二叉查找树(一)之 图文解析 和 C语言的实现
  7. JSONArray遍历
  8. bzoj2754
  9. windows下cocos2dx3.0开发环境及Android编译环境搭建
  10. 使用Visifire+ArcGIS API for Silverlight实现Graphic信息的动态图表显示
  11. FFmpeg and x264 Encoding Guide
  12. 如何简单理解js中this的指向
  13. 《Go语言网络编程》第一章:体系
  14. JQuery弹出层,点击按钮后弹出遮罩层,有关闭按钮【转】
  15. python+selenium十三:破解简单的图形验证码
  16. kbmMW 5.08.01压力测试报告
  17. rpm安装MySQL5.5后配置,在centos5上;mysql编译安装在centos6.5上;
  18. LinkServer--访问远程数据表三种方式
  19. MemSQL Start[c]UP 2.0 - Round 1 E - Three strings 广义后缀自动机
  20. TCP握手及状态图

热门文章

  1. 移动站应该尝试百度MIP的五个原因
  2. 【腾讯bugly干货分享】HTML 5 视频直播一站式扫盲
  3. 小白解决CENTOS7错误:Cannot find a valid baseurl for repo: base/7/x86_6
  4. Centos6.5下编译安装mysql 5.6
  5. 【手把手】JavaWeb 入门级项目实战 -- 文章发布系统 (第十二节)
  6. RMS:不能对生产服务器使用测试清单
  7. iOS之延时执行(睡眠)的几种方法
  8. mysql查询性能优化
  9. 万向节锁(Gimbal Lock)的理解
  10. SpringMVC(关于HandlerMapping执行流程原理分析)