onWhenAddingFileFailed never fires
yauhenkashko opened this issue · 4 comments
yauhenkashko commented
There are 2 issues with _failFilterIndex
that cause onWhenAddingFileFailed
never to fire.
- If any filter fails the
_failFilterIndex
will never change to a value greater than0
due to the Incorrectif (this._failFilterIndex) {
condition:
protected _isValidFile(file: FileLikeObject, filters: FilterFunction[], options: FileUploaderOptions): boolean {
this._failFilterIndex = -1;
return !filters.length ? true : filters.every((filter: FilterFunction) => {
----> if (this._failFilterIndex) { <----
this._failFilterIndex++;
}
return filter.fn.call(this, file, options);
});
}
- When the
_isValidFile
returnsfalse
(if any filter fails),this._failFilterIndex
is always0
(as we saw in the previous_isValidFile
issue - see above). Сonsidering the previous statement, the invalid conditionif(this._failFilterIndex)
with a value of0
will not fire _onWhenAddingFileFailed:
addToQueue(files: File[], _options?: FileUploaderOptions, filters?: [] | string): void {
...
if (this._isValidFile(temp, arrayOfFilters, options)) {
...
} else {
-----> if (this._failFilterIndex) { <-----
const filter = arrayOfFilters[ this._failFilterIndex ];
this._onWhenAddingFileFailed(temp, filter, options);
}
}
});
...
}
As the result, the FileUploader's onWhenAddingFileFailed
is not called.
Was broken during the upgrade to angular 11:
yauhenkashko commented
Pull request with the fix.
githubEnjoer1 commented
I also encountered this problem.
elementalTIMING commented
are there any news when this bug is gonna be fixed? Is there any workarround available?
yauhenkashko commented
Until the fix is released I used the following temporary workaround:
//TODO: Remove WorkaroundFileUploader (file-uploader-fix.class.ts) and replace with FileUploader when 0 index issue with failFilterIndex will be resolved
export class WorkaroundFileUploader extends FileUploader {
constructor(options: FileUploaderOptions) {
super(options);
//Add filter stub at 0 index to avoid the issue
this.options.filters?.unshift({ name: "zero-index-filter-stub", fn: () => true });
}
//Override FileUploader's _isValidFile because it never changes value of the failFilterIndex to greater then 0 due to incorrect if condition
protected _isValidFile(file: FileLikeObject, filters: FilterFunction[], options: FileUploaderOptions): boolean {
this._failFilterIndex = filters.length
? filters.findIndex((filter: FilterFunction) => !filter.fn.call(this, file, options))
: -1;
return this._failFilterIndex == -1;
}
}