Files not removed correctly in file upload
I'm submitting a ... (check one with "x")
[X] bug report => Search github for a similar issue or PR before submitting
[ ] feature request => Please check if request is not on the roadmap already https://github.com/primefaces/primeng/wiki/Roadmap
[ ] support request => Please do not submit support request here, instead see http://forum.primefaces.org/viewforum.php?f=35
Plunkr Case (Bug Reports) http://www.primefaces.org/primeng/#/fileupload
Current behavior When we select multiple files for upload, and try deleting a file other than first, it is not removed; instead first file is removed.
Expected behavior Selected file should get deleted.
Minimal reproduction of the problem with instructions
- Select 2 or more files in file upload.
- Delete second file. (first file will get deleted!!)
What is the motivation / use case for changing the behavior? In fileupload.js file, the index i that is passed as an argument to remove function is not defined anywhere in html. let i = index needs to be added in the code.
Please tell us about your environment:
- Angular version: 2.0.X
- PrimeNG version: 1.1.0
- Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]
-
Language: [all | TypeScript X.X | ES6/7 | ES5]
-
Node (for AoT issues):
node --version=
Could it be that you have single selection mode? I have the same issue with single selection mode and surprisingly I can select multiple images with multiple=false. When I delete the last selected item then the first disappears like you described. I don't know if this is connected.
the primeng code is wrong. If you check their fileupload,js file's template code, they have passed i as argument to remove() function but have not initialized 'i' in ngFor.
As already mentioned, the index (i) passed to remove is not initialized. The *ngFor needs to be:
*ngFor="let file of files; let i = index;".
In the meantime, you can work around this by using @ViewChild() in your component to get a reference to the FileUploader (I'm naming it fileUpload in the below example), then specify a custom file template:
<template let-file let-i="index" pTemplate="file">
<div class="ui-fileupload-row">
<div><img [src]="file.objectURL" *ngIf="fileUpload.isImage(file)" [width]="fileUpload.previewWidth" /></div>
<div>{{file.name}}</div>
<div>{{fileUpload.formatSize(file.size)}}</div>
<div><button type="button" icon="fa-close" pButton (click)="fileUpload.remove(i)"></button></div>
</div>
</template>
Do you have a full example of using the template, because I want to use the same for changing the filesize string, which is working but when I click on the remove button I get 'Cannot read property 'remove' of undefined'
This is my current code:
<p-fileUpload name="fileUpload" url="{{url}}" multiple="multiple"
(onBeforeSend)="onBeforeSend($event)" (onError)="onError($event)"
chooseLabel="XML-bestanden kiezen" cancelLabel="Annuleren" accept=".xml" maxFileSize="50000000">
<template let-file let-i="index" pTemplate="file">
<div class="ui-fileupload-row">
<div>{{file.name}}</div>
<div>{{fileUploadFormatSize(file.size)}}</div>
<div><button type="button" icon="fa-close" pButton (click)="fileUpload.remove(i)"></button></div>
</div>
</template>
</p-fileUpload>
@cagataycivici , @Mrtcndkn The fix is easy, please fix it, I have to fix it manually every time I update primeng
Fixed by adding the index definition to ngFor, thank you.