2

我需要在选择时获取输入的文件名并将其保存到数据库中,但它似乎唯一显示的是文件的假路径。

//component.html
<div class="col s12 file-field input-field" style="margin-left: -10px">
    <div>
        <input type="file" name="cat" [(ngModel)]="subf" ng2FileSelect [uploader]="uploader2"/>
    </div>
    <div class="file-path-wrapper" style="margin-right: -10px">
        <input class="file-path validate" name="banner" ngModel placeholder="Seleccione una Imagen" type="text">
        <label for="banner" style="margin-left: 10px">Banner</label>
    </div>
</div>

//component.ts
var subf: any;
console.log(subf);

我得到的输出是C:\fakepath\image.png,我只需要image.png

4

1 回答 1

2

你应该有一个带有点击事件的按钮。

<input style="display: none" type="file" (change)="onFileChanged($event)" #fileInput>
  <button type="button" (click)="fileInput.click()">Select File</button>

并在 ts 文件中

onFileChanged(event) {
 console.log( event.target.files[0].name) );
 }
于 2018-12-11T02:26:43.050 回答