PhantomJS - Upload a file without submitting a form

Can I upload a file to a specific page using PhantomJS without manually submitting the form? I think something is possible using Content-Type: multipart / form-data.

The example https://github.com/ariya/phantomjs/blob/master/examples/imagebin.js works fine, but I want to send the file directly to the POST request without interacting with any element.

Any suggestion?

Thank you very much

+6
source share
2 answers

Downloading files can be done via AJAX (with xhr2 - if you need to support older browsers, use something like jQuery-File-Upload ; a good tutorial here is about using it only to make part of the download, and not use the Blueimp interface. ) This is due to the fact that the "no manual filing form" part of your question. You still need to interact with the file upload element to select the file, and that is where you use page.uploadFile() .

To do this exclusively from JavaScript, without “interacting with any element on the page”, you can use page.evaluate () to run some custom JavaScript. Then he can use the File Reader API to find the file on the local disk, save it in the blob, and then load this block using AJAX. WebKit in PhantomJS (roughly equivalent to Chrome 13) should work, as the FileReader API seems to exist with Chrome 6.

(BTW, if all you wanted to do was upload the file to the server using a headless script, PhantomJS is full and you can use curl . But I guess you want to use PhantomJS for some other reason!)

+3
source

The solution that I used at the end is a combination of NodeJS and PhantomJS, in case I need to download something that I reprogrammed the process and uploaded files (files) using the "NodeJS" request module, and then sent back PhantomJS displays page to be processed manually.

0
source

All Articles