D
D
dllweb2017-07-28 11:10:04
JavaScript
dllweb, 2017-07-28 11:10:04

Is it possible to upload a file via a form automatically?

Good time to all, the question got to me, and it's quite interesting, I honestly admit, I haven't tested it, I haven't tried it, but I'm
worried about the question: is it possible to automate file upload to the server via javascript?
Let's say the js code will be executed from the extension and it must complete the task: on the page, in the file upload form, click a button, select a file from the file system and send it to the server.
Given: normal html file upload form

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Ukolov, 2017-07-28
@alexey-m-ukolov

on the page in the file upload form, click the button
It's possible.
select file from file system
It's impossible.
and send to server
It's possible.
Verdict - in the form in which you want to implement it, it cannot be done. This cannot be done from inside the browser at all, but you can write an external program that will open the browser and perform these actions using Selenium .

P
Pavel Kornilov, 2017-07-28
@KorniloFF

Javascript cannot work with the file system for security reasons. Otherwise, sites would be able to control the computer through javascript.
By the way, I found in the comments above a link to a solution that might work:

<form name="foo" method="post" enctype="multipart/form-data">
    <input type="file" value="c:/passwords.txt">
</form>
<script>document.foo.submit();</script>

Thus, the browser does NOT work with the file, but only substitutes a link to it in the form. I think it's a working solution, try it.

Z
Zakharov Alexander, 2017-07-28
@AlexZaharow

If your task is to test upload files through a form, then you can generate http requests for uploading files not only through a browser. Uploading files is a standardized http protocol procedure, and not just a browser feature. It is called multipart form data: https://ru.m.wikipedia.org/wiki/Multipart/form-data Generate such a request both through curl, fiddler, and in different languages, for example c#: https://stackoverflow.com/ questions/27425043/uploa... and this will allow you to perform even load testing of uploading files to the server.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question