最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Browse and select files from user hard drive gives undefined in IE - Stack Overflow

programmeradmin4浏览0评论

when I use my input button to browse for the file on the user puter 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 es undefined again

thanks

when I use my input button to browse for the file on the user puter 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 es undefined again

thanks

Share Improve this question edited Aug 19, 2012 at 18:18 setlio asked Aug 17, 2012 at 1:26 setliosetlio 7262 gold badges14 silver badges32 bronze badges 1
  • 1 I have tried this: stackoverflow./questions/3279997/… But it did not work! – setlio Commented Aug 17, 2012 at 1:55
Add a ment  | 

2 Answers 2

Reset to default 6

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/phusick/fkY4k/

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

发布评论

评论列表(0)

  1. 暂无评论