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 相关文章推荐
在js中使用&quot;with&quot;语句中跨frame的变量引用问题
Mar 08 Javascript
js 浮动层菜单收藏
Jan 16 Javascript
JS获取各种宽度、高度的简单介绍
Dec 19 Javascript
js动态修改表格行colspan列跨度的方法
Mar 30 Javascript
jquery插件jquery.confirm弹出确认消息
Dec 22 Javascript
jQuery 操作input中radio的技巧
Jul 18 Javascript
jQuery根据ID、CLASS、等获取对象的实例
Dec 04 Javascript
js禁止浏览器的回退事件
Apr 20 Javascript
BootStrap的两种模态框方式
May 10 Javascript
JavaScript数组去重的多种方法(四种)
Sep 19 Javascript
jQuery实现移动端图片上传预览组件的方法分析
May 01 jQuery
Vue项目如何引入bootstrap、elementUI、echarts
Nov 26 Vue.js
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
php截取后台登陆密码的代码
2012/05/05 PHP
php获取本地图片文件并生成xml文件输出具体思路
2013/04/27 PHP
php计算整个目录大小的方法
2015/06/19 PHP
smarty高级特性之对象的使用方法
2015/12/25 PHP
一份老外写的XMLHttpRequest代码多浏览器支持兼容性
2007/01/11 Javascript
ajax处理php返回json数据的实例代码
2013/01/24 Javascript
JQuery获取或设置ckeditor的数据(示例代码)
2013/11/15 Javascript
js判断客户端是iOS还是Android等移动终端的方法
2013/12/11 Javascript
jQuery之字体大小的设置方法
2014/02/27 Javascript
绑定回车enter事件代码
2014/05/18 Javascript
JavaScript使用Replace进行字符串替换的方法
2015/04/14 Javascript
jQuery实现div横向拖拽排序的简单实例
2016/07/13 Javascript
easy ui datagrid 从编辑框中获取值的方法
2017/02/22 Javascript
Iscrool下拉刷新功能实现方法(推荐)
2017/06/26 Javascript
vue+ElementUI实现订单页动态添加产品数据效果实例代码
2017/07/13 Javascript
Node.js搭建小程序后台服务
2018/01/03 Javascript
jQuery实现滚动到底部时自动加载更多的方法示例
2018/02/18 jQuery
在angular 6中使用 less 的实例代码
2018/05/13 Javascript
在axios中使用params传参的时候传入数组的方法
2018/09/25 Javascript
js使用swiper实现层叠轮播效果实例代码
2018/12/12 Javascript
微信公众号获取用户地理位置并列出附近的门店的示例代码
2019/07/25 Javascript
vue滚动插件better-scroll使用详解
2019/10/18 Javascript
python中stdout输出不缓存的设置方法
2014/05/29 Python
python cs架构实现简单文件传输
2020/03/20 Python
不管你的Python报什么错,用这个模块就能正常运行
2018/09/14 Python
python multiprocessing多进程变量共享与加锁的实现
2019/10/02 Python
Django项目uwsgi+Nginx保姆级部署教程实现
2020/04/19 Python
Python虚拟环境的创建和使用详解
2020/09/07 Python
html5指南-6.如何创建离线web应用程序实现离线访问
2013/01/07 HTML / CSS
html5的canvas实现3d雪花飘舞效果
2013/12/27 HTML / CSS
PHP高级工程师面试问题推荐
2013/01/18 面试题
幼儿师范毕业生自荐信
2013/11/09 职场文书
八项规定整改措施
2014/02/12 职场文书
多媒体编辑专业毕业生求职信
2014/06/13 职场文书
Python爬虫基础之初次使用scrapy爬虫实例
2021/06/26 Python
HTML5中的DOCUMENT.VISIBILITYSTATE属性详解
2023/05/07 HTML / CSS