Javascript error on IE8 and IE9

I get 'this.0.files.0' null or not an object error in IE8 and IE9, and Chrome and Mozila do not cause 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) } } } 

I am not sure why the code above javascript 'this.0.files.0' error is null or not an object

+6
source share
2 answers

IE <10 does not support html5 fileapi, i.e. No HTMLInputElement.FileList you will have to HTMLInputElement.value to get the file name.

+11
source

To get the file name, you can:

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); } } } 
+1
source

Source: https://habr.com/ru/post/943786/


All Articles