Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
196
rated 0 times [  197] [ 1]  / answers: 1 / hits: 199361  / 11 Years ago, wed, april 3, 2013, 12:00:00

I have a problem with the "input tag" in non-IE browsers:


<input type="file" ...

I'm trying to write my uploader, just using JavaScript and ASP.NET.


I have no problem uploading files.


My problem occurred when I wanted to get my files in non-IE browsers with


<input type="file" ...

I do not want to use directly from input because its appearance does not change correctly.


I wrote this code to get files from the hard disk:


function $tag(_str_tag) {
return document.getElementsByTagName(_str_tag);
}

function $create(_str_tag) {
return document.createElement(_str_tag);
}


function $open_file() {
_el_upload = $create("input");
_el_body = $tag("body")[0];
_el_upload.setAttribute("type", "file");
_el_upload.style.visibility = "hidden";
_el_upload.setAttribute("multiple", "multiple");
_el_upload.setAttribute("position", "absolute");
_el_body.appendChild(_el_upload);
_el_upload.click();
_el_body.removeChild(_el_upload);
return _el_upload.files;
}

In IE it works pretty well and returns my files currently.. In Chrome And Firefox, after loading "file input dialog", it can't return any file.


And Opera and Safari are completely out.


I can fix it with this trick, but it's not good basically.


_el_upload.click();
alert();

I think a "callback" or "wait function" may fix this, but I can't handle it.


More From » html

 Answers
8

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.


[#79144] Tuesday, April 2, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tayla

Total Points: 681
Total Questions: 102
Total Answers: 108

Location: Marshall Islands
Member since Tue, Sep 21, 2021
3 Years ago
tayla questions
Fri, Mar 5, 21, 00:00, 3 Years ago
Wed, Oct 28, 20, 00:00, 4 Years ago
Thu, Apr 9, 20, 00:00, 4 Years ago
;