1

I should display the selected file name in label tag instead of displaying in the input tag. Can you please help me to achieve this. Thanks in advance

My code looks like below,

<div *ngFor='let item of list; let i = index'>
  <input #fileInput type="file"  />
  <button type="button" (click)="fileInput.click()">trigger</button>
  <button type="button" (click)="reset(fileInput, i)">Reset</button>
</div>

I have tried changing input tag to label but it's not working. Could you please help me to achieve it. Thanks in advance.

3 Answers 3

3

Try this code.I hope it will helps you.

HTML:

 <div>
        <div>
            <input #fileInput type="file" (change)="fileProgress($event)" />
            <button type="button" (click)="fileInput.click()">trigger</button>
            <button type="button" (click)="reset(fileInput, i)">Reset</button>
        </div>
        <span *ngIf="fileName">{{fileName}}</span>
    </div>

TS:

fileProgress(fileInput: any) {
    let file = fileInput.target.files[0];
     this.fileName = file.name;
    console.log(this.fileName)
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response. It's working for me but its show same file name to all the label once I selected file for the specific row.
You can use for loop for that.
2

try this once..

This will display the name of the file you have selected.

In Template

<div *ngFor='let item of list; let i = index'>
  <span *ngIf="selectedLogoFile[i]">{{selectedLogoFile[i].name}}</span>
  <input style="display: none" type="file" (change)="onFileChanged($event,i)" #fileInput>
  <button type="button" (click)="fileInput.click()">trigger</button>
  <button type="button" (click)="reset(fileInput, i)">Reset</button>
</div>

In Component

  selectedLogoFile: File[]= [];

  onFileChanged(event: any,index: any) {
    this.selectedLogoFile.push(event.target.files[i]);
    console.log(this.selectedLogoFile[i].name);
  }

hope this will give you an idea to solve your issue.. :)

2 Comments

Thanks for the response. It's working for me but its show same file name to all the label once I selected file for the specific row.
@Olive use index value to solve this issue, i have updated the answer chek it once
1
  • You can hide input and wrap it with label
  • Bind variable to input with [(ngModel)]
<label> {{fileName}}
  <input #fileInput type="file" [(ngModel)]="selectedFile" [style.display]="'none'">
</label>

and make getter

get fileName(): string {
  return this.selectedFile ? this.selectedFile.split('/').pop() : 'Select file';
}

then use this getter in any tag like {{ fileName }}

1 Comment

Thanks for the response. I'm using the reactive form so ngModel cannot be used to register form controls with a parent formGroup directive

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.