7

Here i choose multiple images and shows using *ngFor And there I have placed a delete button which is appear in the screenshot, click on delete button i want to delete chosen image from chosen list i tried below code but i not getting proper solution.

This is My UI sceenshot

add.component.html

<button mat-raised-button (click)="fileInput.click()">Select File</button>

<input style="display: none" type="file" (change)="onFileChanged($event)" #fileInput multiple="true">

<div *ngFor="let selected of selectedFile;let index = index">
  <h3>{{selected.name}}</h3>
  <button mat-icon-button (click)="removeSelectedFile(index)">delete</button>
</div>

add.component.ts

selectedFile: File;
ArrayOfSelectedFile = new Array<string>();

onFileChanged(event : any) {
  this.ArrayOfSelectedFile= [];
  this.selectedFile = event.target.files;
  this.ArrayOfSelectedFile.push(event.target.files);
}

removeSelectedFile(index){
  this.ArrayOfSelectedFile.splice(index,1);
}
1
  • if you add console.log(this.ArrayOfSelectedFile) after the splice method, does the element got removed ? Commented Nov 29, 2018 at 10:28

6 Answers 6

18

HTML Code:

<button mat-raised-button (click)="fileInput.click()">Select File</button>

<input style="display: none"  #attachments type="file" (change)="onFileChanged($event)" #fileInput multiple="true">

<div *ngFor="let selected of listOfFiles;let index = index">
  <h3>{{selected}}</h3>
  <button mat-icon-button (click)="removeSelectedFile(index)">delete</button>
</div>

And TS code:

Import this:

import { Component, OnInit, Inject, ViewChild } from '@angular/core';

And Inside your component class:

@ViewChild('attachments') attachment: any;

fileList: File[] = [];
listOfFiles: any[] = [];

onFileChanged(event: any) {
    for (var i = 0; i <= event.target.files.length - 1; i++) {
      var selectedFile = event.target.files[i];
      this.fileList.push(selectedFile);
      this.listOfFiles.push(selectedFile.name)
  }

  this.attachment.nativeElement.value = '';
}



removeSelectedFile(index) {
 // Delete the item from fileNames list
 this.listOfFiles.splice(index, 1);
 // delete file from FileList
 this.fileList.splice(index, 1);
}

If you notice that the Deleted file is not available for upload again for that I have used @ViewChild to reset the value = '', then you can select the deleted file again.

Here is a working StackBlitz example.

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

5 Comments

Instead of using multiple, can we add a button to add a new file with description? Like it should be iterate n number of times when you click on a button. Can we get this scenario done using reactive forms with validation?
@SaiManoj need more clear idea, can you post a question with details, demo?
let me do that and will tag it here. Will make a sample stackblitz too for a brief
@SaiManoj sure also you can mail me the question link..my mail id given in my Prof info
Here is the link for question
0

event.target.files is already an array, which is why you can iterate over it with ngFor. When you assign selectedFile = event.target.files, you are making it an array. Try this:

selectedFile: File;
ArrayOfSelectedFile = new Array<string>();

onFileChanged(event : any) {
  this.selectedFile = event.target.files[0];
  this.ArrayOfSelectedFile = event.target.files;
}

removeSelectedFile(index){
  this.ArrayOfSelectedFile.splice(index,1);
}

<div *ngFor="let selected of ArrayOfSelectedFile;let index = index">
  <h3>{{selected.name}}</h3>
  <button mat-icon-button (click)="removeSelectedFile(index)">delete</button>
</div>

2 Comments

i got error this.ArrayOfSelectedFile.splice is not a function
this.ArrayOfSelectedFile = event.target.files as string[];
0

You can check this Multiple file upload delete, let me know if you want any clarification on same.

7 Comments

Try to upload deleted file again and then check!
@DDD Have you test this--> Try to upload deleted file again
@PrashantPimpale its working like new upload, if you want append then you need to remove this.fileList = [] from fileUpload() and loop event.target.files and push it into this.fileList
@DDD its working from your side? or need any modification
@R.Viral its working like new upload i want to append new images so can you modify answer for that
|
0

You should remove it from a selectedFile array.

this.selectedFile.splice(index, 1);

3 Comments

@VictoryRodniansky selectedFile type is File so it shows error like this [ts] Property 'splice' does not exist on type 'File'
Maybe I am missing something but could you write in html: <div *ngFor="let selected of ArrayOfSelectedFile;let index = index">
@DDD in your case selectedFile is not of type File, but FileList.
0

In your html code

 <label class="form-label">Add Images</label>
      <input type="file"
             class="form-control"
             (change)="onSelectFile($event)"
             multiple accept="image/*" />
    </div>

//this is your ts code

onSelectFile(event) {
if (event.target.files && event.target.files[0]) {
  var filesAmount = event.target.files.length;
  for (let i = 0; i < filesAmount; i++) {
    var reader = new FileReader();

    reader.onload = (event: any) => {
      this.imageurls.push({ base64String: event.target.result, });
     }
    reader.readAsDataURL(event.target.files[i]);
    }
  }
 }

For more details:https://findandsolve.com/articles/how-to-upload-and-remove-multiple-image-using-anular-code-example

Comments

0

There are two better ways to achieve this if you want to avoid explicit for loop.

1- Using spread operator:

    ArrayOfSelectedFile: File[] =[];

    onFileChanged(event : any) {
      this.ArrayOfSelectedFile = [...event.target.files];
    }

    removeSelectedFile(index){
      this.ArrayOfSelectedFile.splice(index,1);
    }

event.target.files is FileList object. To remove a file from a JavaScript FileList is to use the spread operator to convert the FileList to an array. And in removeSelectedFile function, you can use splice function to remove an element of particular index.

2- Using Array.from method:

ArrayOfSelectedFile: File[] =[];

onFileChanged(event : any) {
  this.ArrayOfSelectedFile = Array.from(event.target.files);
}

removeSelectedFile(index){
  this.ArrayOfSelectedFile.splice(index,1);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.