0

I have the next input on a webpage

<input accept="image/jpeg" class="class1" type="file">

And I'm trying to set it's file location via chrome console , I've tried to do the next thing

var Files = ['C:/Users/user/Desktop/dir/toUpload/file.jpg']; 

var upload=document.getElementsByClassName("class1");

upload.files = Files;

But it seems to have no effect on the page at all.

How can I achieve to upload a file that way? (There is no submit button just file input)

1 Answers1

0

I'm 90% sure that this isn't possible from JavaScript. The browser creates a File object after a user gesture. Allowing the page to create files without user gestures would enable random pages to read local files on user's computers.

File objects are generally retrieved from a FileList object returned as a result of a user selecting files using the element, from a drag and drop operation's DataTransfer object, or from the mozGetAsFile() API on an HTMLCanvasElement. In Gecko, privileged code can create File objects representing any local file without user interaction (see Implementation notes for more information.)

https://developer.mozilla.org/en-US/docs/Web/API/File

Kayce Basques
  • 23,849
  • 11
  • 86
  • 120