PhantomJS - uploadFile()



This method is used to handle the file upload done using form in html. PhantomJS does not have a direct way to do so using forms, but the same can be achieved using the uploadFile method. It takes the html tag selector for the file location and the destination where it has to be copied.

Syntax

Its syntax is as follows −

var wpage = require('webpage').create(); 
wpage.uploadFile('input[name = image]', 'path to copy file'); 

Example

The following example shows the use of uploadFile() method.

var wpage = require('webpage').create();  
wpage.open("http://localhost/tasks/file.html", function(status) { 
   console.log(status); 
   wpage.uploadFile('input[name = fileToUpload]', 'output.png');  
   wpage.render("result.png"); 
});

file.html

<html> 
   <head>
      <title>Window 2</title>
   </head> 
   
   <body> 
      <form action = "upload.php" method = "post" enctype = "multipart/form-data" id = "form1">  
         <input type = "file" name = "fileToUpload" id = "fileToUpload"> 
         <input type = "submit" value = "Upload Image" name = "submit"> 
      </form> 
   </body>
   
</html> 

The above program generates the following output.

File Upload
phantomjs_webpage_module_methods.htm
Advertisements