I want to upload a file with a form but I can't fill the <input type="file"> programmatically. Please show me a way to do this. it doesn't differ how. I just want to do that anyway.
-
5You cannot set the file of a file input control programmatically. This is a security feature of the browser.Rory McCrossan– Rory McCrossan2017-04-24 13:15:32 +00:00Commented Apr 24, 2017 at 13:15
-
What can I do else? with other things except HTML?Hameda169– Hameda1692017-04-24 13:17:14 +00:00Commented Apr 24, 2017 at 13:17
-
2I don't think that is possible, considering you could use it to steal people files o.O - that's a form of theft, you know =/Noobit– Noobit2017-04-24 13:17:52 +00:00Commented Apr 24, 2017 at 13:17
-
4Sorry, it's just plain not possible. Imagine all the files I could steal from your PC if it was. The only way a file can be uploaded is if the user selects it.Rory McCrossan– Rory McCrossan2017-04-24 13:18:21 +00:00Commented Apr 24, 2017 at 13:18
-
1Nothing in a browser is going to allow you to touch the user's filesystem progmatically without user input, if you want to upload your own files in bulk, then you'll need to take a different approach.Zachary Craig– Zachary Craig2017-04-24 15:20:01 +00:00Commented Apr 24, 2017 at 15:20
|
Show 2 more comments
3 Answers
You can't add a file to an input field because of security issues.
As a tip, if you wanted to suggest a file, but give them the opportunity to add one themselves, you could have something like this:
<input value="{{ my file name }}">
<label>Add your own file here:</label>
<input type="file">
Comments
Simple answer: you can't. It's a security measure.
If file-inputs were writable I would be able to do something like this:
var input = document.createElement('input');
input.type="file";
input.value="Path/To/Your/Passwords";
myForm.appendChild(input);
myForm.submit();
And that's not wanted and so by default not possible.