首页 > 解决方案 > 如何从angular7中的输入文件获取文件的本地路径?

问题描述

如何从 HTML 中的输入中获取文件的本地路径?

通过下面的代码我收到

'C:\fakepath\fileTest.txt'

我需要一个本地路径来发送到 .netcore Web API 中的控制器,因为我必须在后端创建一个上传的文件。

我的.ts

fileChanged(e) {
    this.file = e.target.files[0];
    console.log('name of file' + this.file.name); // name of file
    console.log('path of file' + e.target.value); // path of file
}

uploadDocument(file) {
    let fileReader = new FileReader();
    fileReader.onload = (e) => {
    console.log(fileReader.result);
};
    fileReader.readAsText(file);
    console.log('sadasdas' + file);
}

我的.html

<div class="col-md-6">
      <p class="my-0">Attachements</p>
      <input type="file" (change)="fileChanged($event)" class="btn btn-secondary" appBootstrapErrors [formControlName]="formControlNames.ATTACHEMENTS">
</div>

标签: htmlangulartypescriptangular7

解决方案


出于安全原因,您无法从用户系统获取文件的文件路径。


推荐阅读