从POST附件下载CasperJS文件

时间:2015-11-24 20:43:10

标签: phantomjs casperjs

我几乎有这个工作,我似乎无法下载该文件。我在这做错了什么?单击按钮"下载销售报告"我应该通过我的console.log()来下载CSV,甚至不会启动。

{
"name":"My name",
"description":"My description!",
"version":"1",
"manifest_version":2,
"browser_action": {
"default_icon": "icon.png"
},
"content_scripts": [
{
    "matches": ["http://www.somePage.com/article*"],
    "js": ["content.js"]
}]}

2 个答案:

答案 0 :(得分:1)

我终于明白了。 Vaviloff拥有我所需要的99%。我只是缺少2个帖子变量。谢谢你的帮助Vaviloff!

    // http://stackoverflow.com/questions/33903418/downloading-a-file-with-casperjs-from-post-attachment
    var casper = require('casper').create({
        verbose: true,
        logLevel: 'debug',
        pageSettings: {
              loadImages:  false         // The WebPage instance used by Casper will
            , loadPlugins: false         // use these settings
            ,  userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'
        }
    });
    var x = require('casper').selectXPath;
    var utils = require('utils');

    casper.on('remote.message', function(message) {
        this.echo('LOG: ' + message);
    });

    casper.start()
    .open('http://clintonoh.ddti.net/Reports.aspx?ActiveTab=Sales')
    .waitForText("Accept")
    .thenClick('#ctl00_ContentPlaceHolder1_btnDisclaimerAccept')
    .waitForText("View Sales")
    .thenClick('#ctl00_ContentPlaceHolder1_WeeklySales_fvSalesReport_btnViewSales')
    .waitForText("Download Sales Report", function(){

        // Adapted from: http://stackoverflow.com/questions/16144252/downloading-a-file-that-comes-as-an-attachment-in-a-post-request-response-in-pha
        var res = this.evaluate(function() {

            document.getElementById('__EVENTTARGET').value='ctl00$blSearchLinks' /* Was missing these 2 */
            document.getElementById('__EVENTARGUMENT').value='4'

            var res={};
            f=document.getElementById("aspnetForm");
            var previous_onsubmit = f.onsubmit;
            f.onsubmit = function() {

                //previous_onsubmit();

                //iterate the form fields
                var post={};
                for(i=0; i<f.elements.length; i++) {
                   //console.log(f.elements[i].name + " = " + f.elements[i].value);
                   post[f.elements[i].name]=f.elements[i].value;
                }
                res.action = f.action;
                res.post = post;
                return false; //Stop form submission
            }

            // Trigger the click on the link.
            var link = document.evaluate('//*[@id="ctl00_blSearchLinks"]/li[5]/a', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
            try {

                var e = document.createEvent('MouseEvents');
                e.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
                link.dispatchEvent(e);

            } catch(error){
                console.log(error);
            }

            return res; //Return the form data to casper
        });

        //Start the download
        casper.download(res.action, "./ExportData.csv", "POST", res.post);    
        //casper.capture("./image.png");    

    })

    casper.run();

答案 1 :(得分:-1)

我终于在很长一段时间后得到了答案,我们可以使用download节点模块下载附件,如下所示

const fs = require('fs');

const download = require('download');

download('http://unicorn.com/foo.pdf').then(data => {
    fs.writeFileSync('dist/foo.pdf', data);
});`

Link to Download NPM Module