Tim
Tim

Reputation: 1695

Typescript/Javascript load existing file without user interaction

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!

Upvotes: 0

Views: 230

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075159

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.

Upvotes: 1

Related Questions