FIle上传控件onchange调用javascript函数但不能访问IE7,IE8,IE9中的文件?

时间:2013-03-21 10:46:37

标签: jquery asp.net-mvc

Html代码

   <input type="file" id="fileuplaod" onchange="readURL();"/>

的javascript

 <script type="text/javascript">

    function readURL() {
  var myFileList = document.getElementById("loadfile");
        alert(myFileList.files.length);
        // loop through files property, using length to get number of files chosen
        for (var i = 0; i < myFileList.files.length; i++) {
            // display them in the div
            alert("Run"); 
            document.getElementById("display").innerHTML += "<br/>" + myFileList.files[i].name;
        }
}
</script>

所有浏览器都在运行但不在IE7,IE8,IE9中运行

2 个答案:

答案 0 :(得分:0)

这可能会对你有所帮助 Click here

以下函数在IE上调用onchange();

时工作正常
function fireEvent(element,event){
if (document.createEventObject){
    // dispatch for IE
    var evt = document.createEventObject();
    return element.fireEvent('on'+event,evt)
}
else{
    // dispatch for firefox + others
    var evt = document.createEvent("HTMLEvents");
    evt.initEvent(event, true, true ); // event type,bubbling,cancelable
    return !element.dispatchEvent(evt);
}

}

答案 1 :(得分:-1)

// FileUpload Control Change Event--
<input type="file" id="fileUpload1" onchange="readPicUrl(this)">

// JavaScript --
function(input)
{
    if(input.files && input.files[0]){ var xPic = new FileRead(); xPic.readAsDataUrl(input.files[0]);  x.onload = function(e){ 
 $("#imageId").attr("src",e.target.result);
 }
 }
}