简体   繁体   English

风俗<input type=“file”>不能在 IE 中工作

[英]Custom <input type=“file”> not working in IE

I'm trying to use a custom < input type="file" > button.我正在尝试使用自定义 < input type="file" > 按钮。 This works in chrome and FF.这适用于 chrome 和 FF。 How do I make it work in IE 10 and above?如何使其在 IE 10 及更高版本中工作?

The problem in IE is that the browse box is not opening. IE 中的问题是浏览框没有打开。

html: html:

<button type="button" id="fileT"><input type="file" id="file"></button>

css: css:

#fileT{
    overflow: hidden;
    position: relative;
}
#fileT input {
    position: absolute;
    opacity: 0.1
}

I've got what you mean: since <input type="file"/> is hard to style, you need a container.我明白你的意思:因为<input type="file"/>很难设计风格,你需要一个容器。 Then try using a <div> instead of a <button> .然后尝试使用<div>而不是<button>

Just ensure you specify height and width since the div content will be absolutely positioned (and hence stripped from the flow).只需确保指定高度和宽度,因为 div 内容将被绝对定位(因此从流程中剥离)。

Running demo :运行演示

<div id="fileT">
    <input type="file" id="file" />
</div>

#fileT{
    overflow: hidden;
    position: relative;
    width: 75px;
    height: 50px;
}
#fileT input {
    position: absolute;
    opacity: 0.5;
}

Just remove that button and try with just input tag.. It works..只需删除该按钮并尝试仅输入标签..它的工作原理..

Like this像这样

<input type="file" id="file" value="Browse"/>

if you want to have your custom button then you have to remove position:absolute and keep opacity:0如果您想拥有自定义按钮,则必须删除position:absolute并保持opacity:0

I think this approach is wrong.我认为这种方法是错误的。 The input field <input type="file"> should not include inside the <button> .输入字段<input type="file">不应包含在<button>

This will work.这将起作用。

<input type="file" id="file">

Try this way:-试试这个方法:-

<input type="file" id="file" multiple="true"/>
    <button type="button" id="fileT">Upload</button>

OR或者

It might be version problem.可能是版本问题。

Updated1:-更新 1:-

This is bug from IE 10 Desktop.To be more specific here's the IE 10 Desktop version:这是 IE 10 桌面版的错误。更具体地说,这里是 IE 10 桌面版:

Version: 10.0.9200.16540
Update Versions: 10.0.4 (KB2817183) 
Product ID: 00150-20000-00003-AA459

Refer This 参考这个

Updated2:- Html:更新 2:- Html:

<div id="wrapper">
    <div id="button">Upload</div>
    <input type="file" id="file" multiple="true"/>
</div>
<div id="notice">Nothing to upload</div>

Css: css:

#button
{
   
    width: 100px;
    height: 50px;
    background-color: #0f0;
}

#wrapper
{
    width: 200px;
    height: 50px;
    overflow: hidden;
    cursor: pointer;
}

Fiddler提琴手

You do not need to surround the input tags with button tags, as the input for file upload automatically creates a browse button for you.您不需要用按钮标签包围输入标签,因为文件上传的输入会自动为您创建一个浏览按钮。 When you click it in IE you are just clicking the empty button and not the browse one created by the input which is why it is not doing anything.当您在 IE 中单击它时,您只是单击了空按钮,而不是由输入创建的浏览按钮,这就是为什么它不执行任何操作的原因。 So instead of:所以而不是:

<button type="button" id="fileT"><input type="file" id="file"></button>

Replace simply with:简单地替换为:

<input type="file" id="fileT">
var input = document.getElementById('Selectfile');
if (!input) {
    input = document.createElement('input');
    input.type = 'file';
    input.id = "Selectfile";
    document.body.appendChild(input);
}
input.onchange = function (e) {
    var file = e.target.files[0]; 
};
input.click();    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM