使用Casperjs下载文件

时间:2015-06-09 12:16:56

标签: javascript file download casperjs

我正在尝试使用casperjs下载csv文件,但我找不到正确的方法!

我有以下简单的php页面,其中包含表单内的图像。当用户单击图像时,正在下载csv文件。这是代码:

<?php

if (isset($_POST['foo_x'])) {
    header('Content-disposition: attachment; filename=file1.csv');
    header('Content-type: text/x-comma-separated-values; charset=iso-8859-7');
}

$foo_x=$_POST['foo_x'];
$foo_y=$_POST['foo_y'];
echo "X=$foo_x, Y=$foo_y ";

?>

<html>
<head>
<body>

<form action='' method=post>
    <input type="text"><br>
    <input type="image" alt='image description' src="image1.jpg" name="foo" id="foo"/>
</form>

</body>
</head>
</html>

为了模拟使用casperjs的下载过程,我编写了以下代码,遗憾的是它不起作用!!

var casper = require('casper').create({
    loadImages:false,
    verbose: true,
    logLevel: 'debug'
});

casper.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X)');

casper.start('http://localhost/input_image_test/', function() {

    this.click('#foo');

    casper.on('resource.received', function(resource) {
        if (resource.stage !== "end") {
            console.log("resource.stage !== 'end'");
            return;
        }
        if (resource.url.indexOf('file1.csv') > -1) {
            console.log("Downloading csv file");
            var fs = require('fs');
            this.download(resource.url, fs.workingDirectory+'/file1.csv');
        }
    });

});

casper.run();

任何人都可以帮忙解决代码问题吗?有什么问题,casperjs代码无法下载file1.csv? 非常感谢!

0 个答案:

没有答案