我需要在选择时获取输入的文件名并将其保存到数据库,但它似乎唯一显示的是文件的假路径。
//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
最佳答案
您应该有一个带有点击事件的按钮。
<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) );
}
关于angular - 如何使用 ngModel 和 ng2-file-upload 获取文件名而不是路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53715939/