javascript - 文件输入转换按钮转换为 Angular 中带有文件名的上传按钮

标签 javascript css angular file-upload angular6

我想上传一个带有浏览按钮的文件,当文件被选中时,浏览按钮应该消失,上传按钮应该出现在所选文件名的旁边。

我应该如何在 Angular 6 中执行此操作?

这是我到目前为止所做的。制作了首屏。

HTML

  <div class="pr-3 pt-2">
    <h4>Choose an excel file to upload</h4>
  </div>
  <div>
    <label for="upload" class="file-upload-label">Browse</label>
    <input type="file" placeholder="Upload file" accept=".xlsx" (change)="incomingfile($event)" class="file-upload-input" id="upload">
  </div>

传入文件事件函数

 incomingfile(event) {
    this.file = event.target.files[0];
    this.filePresent = true;
  }

Image for your reference

最佳答案

基本逻辑-----> DEMO

HTML:

<div class="pr-3 pt-2">
    <h4>Choose an excel file to upload</h4>
</div>
<label *ngIf="!filePresent">
            <input #fileInput  type="file" (change)="incomingfile($event)" />
            <span>Browse File</span>
</label>
<label *ngIf="filePresent">
            <label >{{fileName}}</label>
<button (click)="uploadFile()"><span> Upload</span></button>
</label>

TS:

  file: File;
  filePresent: boolean = false;
  fileName: string = ''

  incomingfile(event) {
    this.file = event.target.files[0];

    this.fileName = this.file.name;

    this.filePresent = true;
  }

  uploadFile() {
    if (this.file) {
      console.log(this.file);
    }
  }

关于javascript - 文件输入转换按钮转换为 Angular 中带有文件名的上传按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51762719/

相关文章:

javascript - 如何在树中的所有节点上运行一些代码?

javascript - 使用 Select Option 下拉菜单的 jQuery Multiple Star Rating 系统

html - 我有一个链接设置为不显示,但它仍然显示

Angular: Material 自定义主题不适用于输入字段

javascript - 在 Angular2 应用程序中使用 bootstrap 时出现问题

javascript - 在不更改 src URL 的情况下,浏览器缓存 javascript 文件失效

html - 如何为每个段落 <p> 后面的反增量元素添加水印?

html - 内联 block 与边距 : 0 auto

scroll - 平滑滚动 angular2

javascript - 如何让按钮每次点击都保持移动?