0

Is it possible to get the full path of the selected file using pure javascript or angular 2? I need the user to pick the excel file which I then need to send to c# WEB API controller method for further processing.

For now I got this:

HTML

<input type="file" id="selectedFile"  class="button-pop-up-footer1">

js

var selectedFilePath = document.getElementById("selectedFile").value;

But when i select the file located at: C:\users\owner\desktop\myfile.xls, I get the path as C:\fakepath\myfile.xls.

Anyone got ideas on how to resolve this and get the full path to the file without compromising the security?

Željko Krnjić
  • 2,356
  • 2
  • 17
  • 24

1 Answers1

3

Is it possible to get the full path of the selected file using pure javascript or angular 2?

No. It would be a serious security problem.

I need the user to pick the excel file which I then need to send to c# WEB API controller method for further processing.

That's fine, you don't need the real path for that. Just use an input type="file" and post the form to your controller (or use an up-to-date fetch or XMLHttpRequest to do it with ajax — Angular's http service should be up to the task on a modern browser).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    Could you share some ajax or better angular 2 code that would post the file to the WEB API? Or a link to some tutorial where I can see the implementation. Thanks for the answer btw :D – Željko Krnjić Jan 29 '18 at 08:57
  • @ŽeljkoKrnjić: No, don't really do Angular, but I can't imagine such a thing would be difficult to find. – T.J. Crowder Jan 29 '18 at 08:58