Rai*_*ain 2 javascript upload phantomjs
我的服务器上有一个php文件来上传文件.我试图直接使用该文件上传,但它很成功.
然后我写了一个简单的phantomjs脚本,使用phantomjs 2.1.1版在ubuntu上运行它,但它不成功.没有显示错误但文件未上传.
以下是php文件:
<?php
if(isset($_FILES["files"]))
{
$files = $_FILES["files"];
if($files["name"] != '')
{
$fullpath = "./".$files["name"];
if(move_uploaded_file($files['tmp_name'],$fullpath))
{
echo "<h1><a href='$fullpath'>OK-Click here!</a></h1>";
}
}
echo '<html><head><title>Upload files...</title></head><body><form method=POST enctype="multipart/form-data" action=""><input type="file" name="files"><input type="submit" value="Upload"></form></body></html>';
return;
}
?>
Run Code Online (Sandbox Code Playgroud)
和简单的phantomjs脚本上传文件
var webPage = require('webpage');
var page = webPage.create();
var testindex = 0, loadInProgress = false;
page.onLoadStarted = function() {
loadInProgress = true;
console.log("load started");
};
page.onLoadFinished = function() {
loadInProgress = false;
console.log("load finished");
};
function load()
{
page.open("http://myserver.com/upload.php?cmd=upload")
}
function upload()
{
page.uploadFile('input[name=files]', '/home/user/Desktop/log_no_debug.png');
page.render("result.png")
}
var steps = [
load,
upload
]
interval = setInterval(function() {
if (!loadInProgress && typeof steps[testindex] == "function") {
//console.log("step " + (testindex + 1));
steps[testindex]();
testindex++;
}
if (typeof steps[testindex] != "function") {
//console.log("test complete!");
phantom.exit();
}
}, 50);
Run Code Online (Sandbox Code Playgroud)
下面是从upload.js脚本呈现的result.png

原因文件未上传是因为您只在客户端选择它,但不发送到服务器 - 您不在PhantomJS脚本中提交表单.
我已经修改了你的脚本,寻找评论:
var webPage = require('webpage');
var page = webPage.create();
var testindex = 0, loadInProgress = false;
page.onLoadStarted = function() {
loadInProgress = true;
console.log("load started");
};
page.onLoadFinished = function() {
loadInProgress = false;
console.log("load finished");
};
function load()
{
page.open("http://test/upload.php?cmd=upload")
}
function upload()
{
loadInProgress = true;
page.uploadFile('input[name=files]', '/path/to/file.ext');
// Here we submit the form to the server
page.evaluate(function(){
// document.querySelectorAll("input[type=submit]")[0].click();
document.querySelectorAll("form")[0].submit();
});
}
// And only after the page has reloaded it is time to make a screenshot
function finish()
{
page.render("result.png");
}
var steps = [
load,
upload,
finish
]
interval = setInterval(function() {
if (!loadInProgress && typeof steps[testindex] == "function") {
//console.log("step " + (testindex + 1));
steps[testindex]();
testindex++;
}
if (typeof steps[testindex] != "function") {
//console.log("test complete!");
phantom.exit();
}
}, 500);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1940 次 |
| 最近记录: |