0

I've seen many examples making use of

<input type="file" id="file"/>

and then something like

let $img: any = document.querySelector('#file');

if (typeof (FileReader) !== 'undefined') {
    let reader = new FileReader();

    reader.onload = (e: any) => {
      this.pdfSrc = e.target.result;
    };

    reader.readAsArrayBuffer($img.files[0]);
}

However, I know the file path and I just want to get that without any user interaction. In the example, $img.files[0] is a Blob, so it would seem that I would need to create a new Blob or File object from my known file path, but I can't figure out how to do that. I do not want to create a new file, just an object from the existing file. I would think this would be quite straight-forward, so I'm hoping I'm just missing something easy. Thanks!

4
  • 2
    It sounds like it would be a security hole if the browser let javascripts just load any file it liked, wouldn't it? Commented Feb 21, 2018 at 17:54
  • There is no way to select a file without the user selecting it. Commented Feb 21, 2018 at 17:54
  • 2
    So it sounds like the correct way to do this is to have javascript load the file from the server. Commented Feb 21, 2018 at 17:57
  • 1
    My search-fu must be weak today. I'm sure I've seen this question a dozen times; not finding them, though. Hopefully someone else will. Commented Feb 21, 2018 at 18:02

1 Answer 1

1

No, you cannot do that. The user has to identify the file to load (via the input type="file", drag-and-drop, etc.). You cannot pre-specify the filename, nor initiate the file load, without the user's intervention. It would be a massive security hole.

You can load files from your server, or from other servers allowing your origin via Cross-Origin Resource Sharing, but not from the user's machine.

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

Comments

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.