how to get files from <input type='file' …/> (Indirect) with javascript

后端 未结 3 882
闹比i
闹比i 2020-12-05 06:43

I have problem with \"input tag\" in non IE browsers

I\'m trying to write my uploader , just using javascript a

相关标签:
3条回答
  • 2020-12-05 06:54

    Based on Ray Nicholus's answer :

    inputElement.onchange = function(event) {
       var fileList = inputElement.files;
       //TODO do something with fileList.  
    }
    

    using this will also work :

    inputElement.onchange = function(event) {
       var fileList = event.target.files;
       //TODO do something with fileList.  
    }
    
    0 讨论(0)
  • 2020-12-05 07:06

    Above answers are pretty sufficient. Additional to the onChange, if you upload a file using drag and drop events, you can get the file in drop event by accessing eventArgs.dataTransfer.files.

    0 讨论(0)
  • 2020-12-05 07:13

    If you are looking to style a file input element, look at open file dialog box in javascript. If you are looking to grab the files associated with a file input element, you must do something like this:

    inputElement.onchange = function(event) {
       var fileList = inputElement.files;
       //TODO do something with fileList.  
    }
    

    See this MDN article for more info on the FileList type.

    Note that the code above will only work in browsers that support the File API. For IE9 and earlier, for example, you only have access to the file name. The input element has no files property in non-File API browsers.

    0 讨论(0)
提交回复
热议问题