PhantomJS - uploadFile()



此方法用於處理 HTML form 中完成的檔案上傳。PhantomJS 沒有直接方法使用表單完成此操作,但可以使用上傳檔案方法來實現此目的。它獲取檔案位置的 html 標籤選擇器和必須複製到的目標位置。

語法

其語法如下 −

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

示例

以下示例演示了 uploadFile() 方法的使用。

var wpage = require('webpage').create();  
wpage.open("https:///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> 

以上程式會生成以下 輸出

File Upload
phantomjs_webpage_module_methods.htm
廣告
© . All rights reserved.