0

How can I get the local path of the file from input in HTML?

By the code below I received

'C:\fakepath\fileTest.txt'

I need a local path to send to my controller in .netcore web API because I have to create a uploaded file on the backend.

My .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);
}

My .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>

1 Answer 1

1

You cant get the file path of a file from an user system for security reasons.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok. So how can I send the file to my controller? My controller has parameter with DTO which has a prop like IFormFile Attachements {get;set;}. How can I send the file by input in HTML in angular7?
You need to create a FormData and append the file to it, then you can send your file to your backend

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.