0

Is there a way to get the file path of the file that the user has uploaded?

I have this input field (Angular):

<div class="input-group mb-3">
    <input #select type="file" class="form-control" id="inputGroupFile02" (change)="readFile(select)" accept=".txt">
    <label class="input-group-text" for="inputGroupFile02">Upload</label>
</div>

I'm able to get the file name and its content this way:

async readFile(select: HTMLInputElement) {
 this.fileToUpload = select?.files?.item(0) || null;

 const formData: FormData = new FormData();
 if (this.fileToUpload) {
   formData.set("name", this.fileToUpload.name);
   formData.set("file", await this.fileToUpload.text().then(fileContent => {
     return fileContent
   }));
  }

Is there a way I can get the file path so that i can store it in a database? I could only manage to get "fakepath/fileName" and I know that this is done for security reason, but there's a secure way to get the path? (Maybe with some NodeJS API?! I tried to see if I could do it using the fs module but i didn't find anything useful). Thank you.

3
  • where are you uploading a file? in local folder in node or s3 bucket Commented Jul 7, 2022 at 13:18
  • @PK2995 in a local folder Commented Jul 11, 2022 at 8:02
  • then file path is hostname + path you storing in nodejs for example node folder - uploads then your location of URL localhost:3000/uploads/file-1550484364038.png if you deployed in host use domain name instead of localhost fileupload.com/uploads/file-1550484364038.png Commented Jul 11, 2022 at 13:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.