6

I'm getting 'this.0.files.0' is null or not an object error on IE8 and IE9 and Chrome and Mozila don't throw any errors .

$(function()) {
    var fileType = ['txt' , 'csv' ];
    $('.input_file').find('input [type = "file" ]').live('change', function (e)) {
        $this = $(this) 
        var ext = $this.val() === " " ? " " : this.value.match(/\.(.+)$/)[1];
        if($this.val()) {
            $this.parent().find('label').text.($this[0].files[0].name)  
        }
    }
}

Im not sure why above code throws a javascript error 'this.0.files.0' is null or not an object

13
  • $this = $(this) is missing a semi-colon. Commented Apr 27, 2013 at 18:46
  • 1
    IE8 and 9 has no support for the HTML5 File API. Commented Apr 27, 2013 at 18:47
  • 1
    @TiesonT. mislav.uniqpath.com/2010/05/semicolons . If it's missing anything, it's missing var to make it a local variable Commented Apr 27, 2013 at 19:02
  • apologies , semi-colon is there by mistake I forgot while wiriting code here. Commented Apr 27, 2013 at 19:02
  • Friend , please let me know if this is a trailing comma problem in IE wherein firefox and chrome simple ignores training commas and IE has issues with it, please suggest Commented Apr 27, 2013 at 19:03

2 Answers 2

11

IE < 10 has no support for the html5 fileapi, i.e. no HTMLInputElement.FileList you'll have to parse HTMLInputElement.value to get the file name.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Musa , Please let me know , how this is related with HTML5 as this is a jquery code and then how this needs to be handled ?
@ronan $this[0].files is an instance of a FileList, which is part of the html5 FileApi. This api is not available in IE < 10, instead you can get the file name from the $this[0].value property or $this.val()
1

To get the filename you can do:

var filename = $this[0].files ? $this[0].files[0].name : $this[0].value.match(/[^\/\\]*$/)[0];

or simply:

$this[0].value.match(/[^\/\\]*$/)[0];

Full code:

$(function()) {
    var fileType = ['txt' , 'csv' ];
    $('.input_file').find('input [type = "file" ]').live('change', function (e)) {
        $this = $(this) 
        var ext = $this.val() === " " ? " " : this.value.match(/\.(.+)$/)[1];
        if ($this.val()) {
            var filename = $this[0].files ? $this[0].files[0].name : $this[0].value.match(/[^\/\\]*$/)[0];
            $this.parent().find('label').text.($this[0].files[0].name);
        }
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.