3

Is it possible to upload a file from the DOM? Without any user intervention.

What I mean is something like this:

document.querySelector('#myFileInput').uploadFile('/path/to/file.txt')

I'm using NightmareJS with nightmare-upload and nightmare-iframe-manager, but apparently nightmare-upload doesn't work inside an iframe: https://github.com/rosshinkley/nightmare-upload/issues/9

So I'm looking for a way to do it straight from the document, which I have access to.

And just to be clear, when I say upload, I don't mean talking to the server, I mean doing the same as if a user clicked the "Choose File" button and selected a file.

EDIT:

This is not the same as the suggested question, because I don't really care where the file comes from, it doesn't have to be from disk, it could be (and it's actually preferred to me) that it's generated in code.

The real problema is giving the input A file, any file.

Rodrigo Ruiz
  • 4,248
  • 6
  • 43
  • 75
  • 3
    That sounds... like a major security concern if there is a way to upload files from a user's computer without any user interaction – Matthew Schlachter Mar 08 '18 at 16:00
  • If your suggestion was possible, it would enable the theft of data from the user's computer without their permission. Imagine if yours was a malicious website (or arguably worse, a benign, trusted website which has been hijacked with a malicious payload - it happens) and could just select files at will from the user's disk.... There's a good reason why webpages don't have direct access to the client's device. – ADyson Mar 08 '18 at 16:01
  • I am not sure I understand properly. What is the content of the document.querySelector('#myFileInput') if I user is not intervanting with the page? – nikksan Mar 08 '18 at 16:02
  • 1
    @nikksan — Look at the code example in the question: `.uploadFile('/path/to/file.txt')` — The OP is asking how their JavaScript can pick a file of the JS author's choice from the visitor's disk. – Quentin Mar 08 '18 at 16:03
  • It could be a code generated file, that would also be ok – Rodrigo Ruiz Mar 08 '18 at 18:41

0 Answers0