js 实现 input type="file" 文件上传示例代码


Posted in Javascript onAugust 07, 2013

在开发中,文件上传必不可少,<input type="file" /> 是常用的上传标签,但是它长得又丑、浏览的字样不能换,我们一般会用让,<input type="file" />隐藏,点其他的标签(图片等)来时实现选择文件上传功能。
看代码:

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script src="js/jquery/jquery-1.8.2.min.js" type="text/javascript"></script> 
<style type="text/css"> 
._box 
{ 
width: 119px; 
height: 37px; 
background-color: #53AD3F; 
background-image: url(images/bg.png); 
background-repeat: no-repeat; 
background-position: 0 0; 
background-attachment: scroll; 
line-height: 37px; 
text-align: center; 
color: white; 
cursor: pointer; 
} 
.none 
{ 
width: 0px; 
height: 0px; 
display: none; 
} 
</style> 
<title>js 实现 input file 文件上传 /></title> 
</head> 
<body> 
<form id="form1" runat="server" method="post" enctype="multipart/form-data"> 
<div> 
<div class="_box">选择图片</div> 
</div> 
<div class="none"> 
<input type="file" name="_f" id="_f" /> 
</div> 
</form> 
</body> 
</html> 
<script type="text/javascript"> 
jQuery(function () { 
$("._box").click(function () { 
$("#_f").click(); 
}); 
}); 
</script>

但是在火狐和一些高版本的浏览器中后台可以获取到要上传的文件,一些低版本的浏览器压根就获取不到Request.Files
查阅资料,有说改成这样的:
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script src="js/jquery/jquery-1.8.2.min.js" type="text/javascript"></script> 
<style type="text/css"> 
._box 
{ 
width: 119px; 
height: 37px; 
background-color: #53AD3F; 
background-image: url(images/bg.png); 
background-repeat: no-repeat; 
background-position: 0 0; 
background-attachment: scroll; 
line-height: 37px; 
text-align: center; 
color: white; 
cursor: pointer; 
} 
.none 
{ 
width: 0px; 
height: 0px; 
display: none; 
} 
</style> 
<title>js 实现 input file 文件上传 /></title> 
</head> 
<body> 
<form id="form1" runat="server" method="post" enctype="multipart/form-data"> 
<div> 
<div class="_box">选择图片</div> 
</div> 
<div class="none"> 
<input type="file" name="_f" id="_f" /> 
</div> 
</form> 
</body> 
</html> 
<script type="text/javascript"> 
jQuery(function () { 
$("._box").click(function () { 
return $("#_f").click(); 
}); 
}); 
</script>

加了一个return关键字,兼容性提高了不少,但是有的浏览器还是不好用。
我们发现只有手动点击<input type="file" />后台就一定能获取到要上传的文件
于是我们可以透明<input type="file" />
修改代码如下:
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<style type="text/css"> 
._box 
{ 
position: relative; 
width: 119px; 
height: 37px; 
background-color: #53AD3F; 
background-image: url(images/bg.png); 
background-repeat: no-repeat; 
background-position: 0 0; 
background-attachment: scroll; 
line-height: 37px; 
text-align: center; 
color: white; 
cursor: pointer; 
overflow: hidden; 
z-index: 1; 
} 
._box input 
{ 
position: absolute; 
width: 119px; 
height: 40px; 
line-height: 40px; 
font-size: 23px; 
opacity: 0; 
filter: "alpha(opacity=0)"; 
filter: alpha(opacity=0); 
-moz-opacity: 0; 
left: -5px; 
top: -2px; 
cursor: pointer; 
z-index: 2; 
} 
</style> 
<title>js 实现 input file 文件上传 /></title> 
</head> 
<body> 
<form id="form1" runat="server" method="post" enctype="multipart/form-data"> 
<div> 
<div class="_box"> 
<input type="file" name="_f" id="_f" /> 
选择图片 
</div> 
</div> 
</form> 
</body> 
</html>

我们点击选择图片实际点击了不透明度为0的 <input type="file" />,单用户切看不到 <input type="file" />后台亦可以获取到要上传的文件了。
ok
总结:
用一个不透明度为0的 <input type="file" />盖在要用户可见的标签(或图片等)上,让用户点击。
用 width height line-height font-size 来控制<input type="file" />右侧浏览按钮的大小。
用 left top (right 、 bottum)来控制<input type="file" />右侧浏览按钮的位置,可以设置为负值。
用z-index来设置它们的层覆盖关系。
form 必须有enctype="multipart/form-data"标记才能上传文件
Javascript 相关文章推荐
用倒置滤镜把div倒置,再把table倒置。
Jul 31 Javascript
JS 遮照层实现代码
Mar 31 Javascript
js 通过html()及text()方法获取并设置p标签的显示值
May 14 Javascript
jQuery调取jSon数据并展示的方法
Jan 29 Javascript
JavaScript+html5 canvas绘制的小人效果
Jan 27 Javascript
详解Webwork中Action 调用的方法
Feb 02 Javascript
jqGrid用法汇总(全经典)
Jun 28 Javascript
angular $watch 一个变量的变化(实例讲解)
Aug 02 Javascript
VUE element-ui 写个复用Table组件的示例代码
Nov 18 Javascript
VUE接入腾讯验证码功能(滑块验证)备忘
May 07 Javascript
vue实现带复选框的树形菜单
May 27 Javascript
vue-i18n实现中英文切换的方法
Jul 06 Javascript
uploadify在Firefox下丢失session问题的解决方法
Aug 07 #Javascript
firefox浏览器不支持innerText的解决方法
Aug 07 #Javascript
javascript的内存管理详解
Aug 07 #Javascript
javaScript函数中执行C#代码中的函数方法总结
Aug 07 #Javascript
JS 按钮点击触发(兼容IE、火狐)
Aug 07 #Javascript
js 编码转换 gb2312 和 utf8 互转的2种方法
Aug 07 #Javascript
Javascript页面添加到收藏夹的简单方法
Aug 07 #Javascript
You might like
当年上海收录机产品生产,进口和价格情况
2021/03/04 无线电
探讨如何在PHP开启gzip页面压缩实例
2013/06/09 PHP
thinkphp区间查询、统计查询与SQL直接查询实例分析
2014/11/24 PHP
php接口数据加密、解密、验证签名
2015/03/12 PHP
分享自定义的几个PHP功能函数
2015/04/15 PHP
php创建桌面快捷方式实现方法
2015/12/31 PHP
WordPress开发中短代码的实现及相关函数使用技巧
2016/01/05 PHP
php使用正则表达式去掉html中的注释方法
2016/11/03 PHP
PHP微信H5支付开发实例
2018/07/25 PHP
php设计模式之代理模式分析【星际争霸游戏案例】
2020/03/23 PHP
一段实时更新的时间代码
2006/07/07 Javascript
用JavaScript脚本实现Web页面信息交互
2006/12/21 Javascript
innerText和innerHTML 一些问题分析
2009/05/18 Javascript
基于NodeJS的前后端分离的思考与实践(五)多终端适配
2014/09/26 NodeJs
基于JavaScript实现TAB标签效果
2016/01/12 Javascript
jquery插入兄弟节点的操作方法
2016/12/07 Javascript
浅谈vue-cli 3.0.x 初体验
2018/04/11 Javascript
vue将毫秒数转化为正常日期格式的实例
2018/09/16 Javascript
js实现搜索提示框效果
2020/09/05 Javascript
详解vue 组件注册
2020/11/20 Vue.js
[01:10]DOTA2 Supermajor:英雄,由我们见证
2018/05/14 DOTA
Python 爬虫多线程详解及实例代码
2016/10/08 Python
利用Tkinter和matplotlib两种方式画饼状图的实例
2017/11/06 Python
Python登录并获取CSDN博客所有文章列表代码实例
2017/12/28 Python
Python OpenCV获取视频的方法
2018/02/28 Python
python版飞机大战代码分享
2018/11/20 Python
python面向对象法实现图书管理系统
2019/04/19 Python
Python 爬虫批量爬取网页图片保存到本地的实现代码
2020/12/24 Python
c语言常见笔试题总结
2016/09/05 面试题
大学生职业生涯规划书范文
2014/01/04 职场文书
微信营销策划方案
2014/02/24 职场文书
行政前台岗位职责
2015/04/16 职场文书
预备党员考察意见范文
2015/06/01 职场文书
2016读书月活动心得体会
2016/01/14 职场文书
解决golang结构体tag编译错误的问题
2021/05/02 Golang
一篇文章弄清楚Ajax请求的五个步骤
2022/03/17 Javascript