Failed to set an indexed property on 'FileList': Index property setter is not supported.v

Jeffri Andriyanto picture Jeffri Andriyanto · Oct 3, 2017 · Viewed 11.6k times · Source
angular.module('myApp')
    .directive('fileModel', ['$parse', 'uploadService','messageService','CONF', function ($parse, uploadService,messageService,CONF) {
        return {
            restrict: 'A',
            link: function (scope, element, attrs) {
                var model = $parse(attrs.fileModel);
                var modelSetter = model.assign;
                element.bind('change', function () {

                    var file = element[0].files[0];

                    var fd = new FormData();
                    fd.append('file', file);
                    console.log(fd)

                    self.isDisabled = true;


                       ParkingService.uploadImage(fd).then(
                            function (response) {
                                response = JSON.parse(response);
                                element[0].files[1] = response;
                                console.log(response)
                                    if (response.message !== 'ERROR') {
                                        // self.image = response.result;
                                            messageService.toasterMessage(CONF.TOASTER_TOP_RIGHT,CONF.TOASTER_SUCCESS,"Success Upload File");
                                    }
                                    else {
                                        messageService.toasterMessage(CONF.TOASTER_TOP_RIGHT,CONF.TOASTER_ERROR,response.result);

                                    }
                                }
                        );

                    scope.$apply(function () {
                        modelSetter(scope, element[0].files);
                    });
                });
            }
        };
    }]);

how to set response in this variable?

element[0].files[1] = response;

it works in safari or older chrome. but not support in new chrome..

Answer

martypdx picture martypdx · Oct 11, 2017

Both Chrome and Firefox now disallow setting new indexes on NodeList and FileList. I suspect this has to do with recent changes to make these "array-like" structures more compatible in general with JavaScript and is general direction where browsers are headed.

Running in non-strict mode reverts the behavior, but is not feasible in a transpiled JavaScript environment - or likely desirable.

Best option would be to convert the FileList to an array using const files = Array.from(element[0].files) or just const files = [...element[0].files] (depending on what browsers you support and what transpilation you're using).