Browse and select files from user hard drive gives undefined in IE

拜拜、爱过 提交于 2019-12-06 10:04:51

问题


when I use my input button to browse for the file on the user computer it works on FF, IE9 and Chrome. But when I am passing the files to the JS function in IE9 I get undefined, while it works perfectly in FF and Chrome.

 <form id="uploadForm" style='display:none;padding:1px;' method="post" enctype="multipart/form-data">
 <input type="file" name="data" id="inFile" size="15" style="display:none" onchange="handleFiles(this.files)"/>


function handleFiles(files){
//doing something with the files
}



 //In IE files is undefined

I have also tried to use

    dojo.connect(dojo.byId("uploadForm").data, "onchange", function(evt){
        handleFiles(this.files);
    });

<form id="uploadForm" method="post" enctype="multipart/form-data">
<input type="file" name="data" id="inFile" size="15" style="display:none"/>

This.files comes undefined again

thanks


回答1:


IE9 does not support multiple files upload and it does not have files property. You will have to rely on value property and parse a filename from the path it provides.

My solution:

  1. Pass this instead of this.files into handleFiles() function:

    <input type="file" onchange="handleFiles(this)">
    
  2. Start your handleFiles() function like this:

    function handleFiles(input){
        var files = input.files;
        if (!files) {
            // workaround for IE9
            files = [];            
            files.push({
                name: input.value.substring(input.value.lastIndexOf("\\")+1),
                size: 0,  // it's not possible to get file size w/o flash or so
                type: input.value.substring(input.value.lastIndexOf(".")+1)
            });
        }
    
        // do whatever you need to with the `files` variable
        console.log(files);
    }
    

See working example at jsFiddle: http://jsfiddle.net/phusick/fkY4k/




回答2:


Well obviously files is not defined in IE. See here for how to do it with IE.



来源:https://stackoverflow.com/questions/11998664/browse-and-select-files-from-user-hard-drive-gives-undefined-in-ie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!