`

label for input file ie下报拒绝访问

    博客分类:
  • css
 
阅读更多

1. label  for 绑定元素响应事件,例如 点击label可以执行绑定的input 的button的事件,

 

<label for="large_icon_url" class="btn_up file-up-btn">上传大图标</label>
<input type="file" accept="image/*" class="input_f pics" maxnums="1" name="large_icon_url" id="large_icon_url">

 label实现了好看的效果,但是由于ie安全限制问题,会拒绝非file浏览上传文件的访问,所以处理方法应该是让file附在lable的上面,然后是透明处理,直接点击file即可。

 

2. 转上传文件的隐藏处理:

IE input file隐藏不能上传文件解决方法

HTML/CSS 2013-05-30 上传,IE,input,file

又是IE的一个问题,近来是跟IE浏览器磕上了,这个问题发现不少人也遇到过,实在蛋疼。但今天这个不能算是一个bug,因为IE可能是从安全角度上考虑结果导致的。一步步来解读。

普通上传例子

首先普通的文件上传呢,很简单,前端代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>file标签隐藏</title>
 
    </head>
    <body>
        <form action="http://192.168.1.99/upload/upload.php" method="post" enctype="multipart/form-data">
            <input onchange="document.forms[0].submit();" type="file" name="file" />
        </form>
    </body>
</html>

upload.php代码:

1
2
3
echo '<pre>';
print_r($_FILES["file"]);
echo '</pre>';

其实就是打印获取到的文件信息。我们测试一下,选择文件后,提交到PHP页面结果如下:

Array
(
    [name] => 7.jpg
    [type] => image/jpeg
    [tmp_name] => /tmp/php0VkjPG
    [error] => 0
    [size] => 36297
)

能正确获取文件信息,只需要cp下就能保存。

用别的按钮替代file标签

但是默file标签很难看,而且每个浏览器下都有很大差距。因此我们基本都把真正的file标签给隐藏,然后创建一个标签来替代它,比如我们创建一个a标签来替代它,隐藏file标签,单击a标签时触发file标签click弹出选择文件窗口。最终页面代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>file标签隐藏</title>
 
    </head>
    <body>
        <form action="http://192.168.1.99/upload/upload.php" method="post" enctype="multipart/form-data" style="display:none;">
            <input onchange="document.forms[0].submit();" type="file" name="file"  />
        </form>
 
        <a onclick="document.forms[0].file.click();" href="javascript:void(0);" >上传文件</a>
    </body>
</html>

页面上就只看见a标签

点击“上传文件”弹开选择文件的窗口

选择文件后,正确传送文件信息到服务器

这样就完成文件上传了,这个操作在Chrome,FireFox下都正常,IE下有问题。

IE不能上传文件

IE下也能正常弹开选择文件的窗口

但选择文件后,却不能上传,同时还报一个“拒绝访问”错误,如截图中红圈部分

解决IE下不能上传文件的问题

其实这是IE安全限制问题,没有点击file的浏览按钮选择文件都不让上传,既然IE非得要亲自点击,我们可以变通一下,让自定义按钮存在又能真正点击到file标签。解决方案是让file标签盖在a标签上,但file是透明的,这样用户看到的是a标签的外观,实际点击是file标签。如图:

最终页面代码如下:

1
2
3
4
5
<a style="position:relative;display:block;width:100px;height:30px;background:#EEE;border:1px solid #999;text-align:center;"  href="javascript:void(0);" >上传文件
    <form action="http://192.168.1.99/upload/upload.php" method="post" enctype="multipart/form-data">
        <input style="position:absolute;left:0;top:0;width:100%;height:100%;z-index:999;opacity:0;" onchange="document.forms[0].submit();" type="file" name="file"  />
    </form>
</a>

页面:

需要注意几个问题
1、取消a标签onclick事件,因为实际上已经不需要a标签的onclick触发file的click了,而是直接就点击到file;
2、file标签不需要再设置display:none隐藏,而是通过opacity:0让它完全透明,实际它是浮在a标签之上
3、file标签设置position:absolute后要给left:0、top:0,否则file标签不会吻合覆盖a标签导致点击按钮的时候点不到file标签

我们再来测试一下:

点击按钮:

选择文件:

上传成功!

分享到:
评论

相关推荐

    css美化input file按钮的方法

    如果要让浏览按钮更漂亮一点,我们想定义它的背景颜色,甚至想用...偶然看到一篇文章:input file 文件选择框美化 作者是把系统默认的按钮设置透明度为0,再定义一个label标签样式,来覆盖透明掉的按钮。支持IE6\IE7\FF

    jquery.fileEveryWhere.js 一个跨浏览器的file显示插件

    先来看看input type=”file”在chrome,ie,firefox这三个浏览器下表情各异吧。       chrome像是button+label组合,看起差异最大。 ff和ie,是text+button的组合,就外形来看,firefox更标准,事实上firefox存在两...

    js实现图片上传并正常显示

    &lt;label for=thumbnail class=col-md-3&gt;缩略图&lt;/label&gt; &lt;input type=file class=form-control id=thumbnail name=thumbnail&gt; jquery方式,IE不支持,但IE会获得绝对的上传路径信息: function ...

    Login Control

    *New: 1.Login (and Password) label can be in a separate row from the input text box. 2.Login Confirmation is available by option. 3.Add ValidatorStyle for LoginID and Password validators. 4.Can...

    VB编程资源大全(英文源码 网络)

    This could be used for what i used it for in the past for a news program&lt;END&gt;&lt;br&gt;28 , url.zip Worldwide list of URL extensions by country&lt;END&gt;&lt;br&gt;29 , EmailChk.zip This Application checks for ...

    jQuery完全实例.rar

    jQuery 对象访问 each(callback)each(callback) 以每一个匹配的元素作为上下文来执行一个函数。 意味着,每次执行传递进来的函数时,函数中的this关键字都指向一个不同的DOM元素(每次都是一个不同的匹配元素)。 ...

    VB编程资源大全(英文源码 控件)

    1 , vb5dialog.zip This demonstrates how to subclass the Common Dialog Dialogs and manipulate a specific Dialog.&lt;END&gt;&lt;br&gt;2 , cpnl.zip Form_Taskbar is a control for Visual Basic which, once placed...

    VB编程资源大全(英文控件)

    Also includes smooth scrolling for hiding/unhiding.&lt;END&gt;&lt;br&gt;8,tlsGUI.zip TILISOFT GUI ActiveX Controls - ImageLabel, ScrollPanel, SplitPanel, Brief&lt;END&gt;&lt;br&gt;9,recordsetEng.zip This application ...

    html入门到放弃笔记

    1、Microsoft Internet Explorer (IE) 2、Mozilla Firefox 3、Google Chrome 4、Apple Safari 5、Opera Opera(欧朋) 3、主要技术 1、HTML 2、CSS 3、Javascript 2、HTML入门(重点) 1、什么是HTML Hyper ...

    flash标签云 3D效果 PHP插件 by weefselkweekje

    1. Download the zip file and extract the contents. 1. Upload the 'wp-cumulus' folder to your plugins directory (wp-content/plugins/). 1. Activate the plugin through the 'plugins' page in WP. 1. See '...

    org.eclipse.swt.win32

    org.eclipse.swt.browser.IE.class org.eclipse.swt.browser.InputStream.class org.eclipse.swt.browser.LocationAdapter.class org.eclipse.swt.browser.LocationEvent.class org.eclipse.swt.browser....

Global site tag (gtag.js) - Google Analytics