Reputation: 484
Internet explorer is generating this error for the code that follows:
SCRIPT5007: Unable to get value of the property '0': object is null or undefined
for "document.getElementById('files').files[0]
"
It's correct that ...files[0] is null, but I don't care, how can I tell IE not to care?
Thanks
Upvotes: 0
Views: 2148
Reputation: 11
This is very old but I want to answer for anyone who comes here like me
As Dennis says the error indicates that files is an undefined variable and I'm pretty sure that the problem is IE9 doesn't support files property.
As you can see here IE10 is requiered for.
So try this:
var file;
try{
file = document.getElementById('files').files[0];
}catch(error){
file= null;
}
PS: I apologize for my poor English
Upvotes: 1
Reputation: 53301
var file = document.getElementById('files').files[0] || "undefined";
Upvotes: 0
Reputation: 32598
var elem = document.getElementById('files'),
file = elem.files && elem.files[0];
This will short-circuit and return undefined
if files is undefined, otherwise it will return the first file.
Point of clarification, the error indicates that files
is the undefined variable and accessing the property 0
is causing an error. If it were files[0]
itself, the expression would just return undefined
.
Upvotes: 3
Reputation: 8189
Try...
var file;
if (document.getElementById('files').files){
file = document.getElementById('files').files[0];
}
Upvotes: 2