2

I found this question and followed it downloading a file that comes as an attachment in a POST request response in PhantomJs. But the file it saves is an html file and says 404 - File or directory not found. Is there something else I am missing to download this csv file?

Here are the response headers

Cache-Control:private
Content-Disposition:attachment;filename=LenderCompetitionReport.csv
Content-Length:249487
Content-Type:application/csv
Date:Tue, 08 Dec 2015 14:32:47 GMT
Pragma:
Server:Microsoft-IIS/7.0
Set-Cookie:NSC_JOy4ob05etjhas0d2kv5wzcdlqr2zbu=ffffffff09149e4f45525d5f4f58455e445a4a423660;expires=Tue, 08-Dec-2015 14:34:47 GMT;path=/;secure;httponly
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET

And here is my code

casper.waitForSelector(x("//*[@id='ContentBody_ContentBody_btnExport']"),function() {
            casper.then(function() {
                    var res = this.page.evaluate(function() {
                        var res={}; 
                        f=document.forms["frmBase"];
                        f.onsubmit= function() {
                            //iterate the form fields
                            var post={};
                            for(i=0; i<f.elements.length; i++) {
                               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 l = $("#ContentBody_BodyLevelElment_lbtnCsv");
                        l.click();

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

                    //Start the download
                    casper.download(res.action, "LenderCompetitionReport.csv", "POST", res.post);
            });
    });
Community
  • 1
  • 1
Michael St Clair
  • 5,937
  • 10
  • 49
  • 75

2 Answers2

0

I ended up using Selenium instead to download the file as it looks like the website has implemented some kind of security that won't allow me to download it outside of the original post request

Michael St Clair
  • 5,937
  • 10
  • 49
  • 75
0

Download any report file with phantomjs .I am using phantomjs 2.0 for windowsRun below command phatomjs --cookies-file=D:\xampp\htdocs\automation\cookies.txt D:\xampp\htdocs\automation\fetch_example_report.js 2018-12-24T14:44:04+00:00 2018-12-23T14:44:04+00:00

    var page = require('webpage').create();
var fs =  require('fs');
var system = require('system');
var total_arg = system.args;
var uDateTime = total_arg[1];
var pDateTime = total_arg[2];
phantom.cookiesEnabled = true;
var tp = phantom.cookies;
page.open('https://example.com/login.php',function(status) {
    if (status === "success") {
        page.includeJs('https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js', function() {
            page.evaluate(function() {
                $('input[name=email]').val('example@example.com');
                $('input[name=userPass]').val('password');
                $('input[type=submit]').click();
            });
        }); 

        window.setTimeout(function() {
            page.open('https://example.com/daily_report.php', function(status) {
                console.log(uDateTime);
                var phantom_d = new Date();
                console.log(phantom_d);
                var ln=phantom_d.getUTCMonth().toString;
                var phantom_p =new Date(pDateTime);
                var ln_p=phantom_p.getUTCMonth().toString;
                var phantom_month=(ln.length==1)? 0+(phantom_d.getUTCMonth()+1).toString(): (phantom_d.getUTCMonth()+1);
                var phantom_currentDate = phantom_d.getUTCFullYear()+'-'+phantom_month+'-'+phantom_d.getUTCDate();
                                        if(phantom_d.getUTCDate() -1 ==0){
                    var phantom_month_p=(ln_p.length==1)? 0+(phantom_p.getUTCMonth()+1).toString(): (phantom_p.getUTCMonth()+1);
                    var phantom_previousDate = phantom_p.getUTCFullYear()+'-'+phantom_month_p+'-'+(phantom_p.getUTCDate());
                }else{
                    var phantom_previousDate = phantom_d.getUTCFullYear()+'-'+phantom_month+'-'+(phantom_d.getUTCDate() -1);
                }
                var myarray =["m.ID", "email", "custNum", "sForce", "store", "storeType", "orderDate", "fullname", "companyName", "shipFirstname", "shipLastname", "shipAddress1","cancel_code"];
                console.log(phantom_previousDate);
                var res=  page.evaluate(function(cdate,pdate, my_fieldarray) {
                                $("#fields").val(my_fieldarray);   
                                $("select[name=ord_status]").val('30'); 
                                $('input[name=toDate]').val(cdate);
                                $('input[name=fromDate]').val(pdate);
                                $("input[name=order_distinct]").click();
                                $('input[name=export]').click();
                },phantom_currentDate,phan`enter code here`tom_previousDate, myarray);
                window.setTimeout(function() {
                        phantom.exit();
                }, 5000);
            });
        }, 8000);
    }
});
page.onConsoleMessage = function(msg) {
    console.log(msg);
}
page.onFileDownload = function(status){  //window.setTimeout(function() {
    console.log('onFileDownload(' + status + ')'); 
    var phantom_d = new Date(uDateTime);
    var ln_hour=phantom_d.getUTCHours().toString();
    var phantom_hour=(ln_hour.length==1)? 0+ln_hour: ln_hour;
    var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    var fileName = phantom_d.getUTCFullYear()+'-'+months[phantom_d.getUTCMonth()]+'-'+phantom_d.getUTCDate()+'-'+phantom_hour+'.xls';
    return 'D:\xampp\htdocs\automation\'+fileName; 
}
page.onResourceReceived = function(status){ 
    //console.log('onResourceReceived(' + status.stage + ')'); 
    if(status.stage === 'end'){ //phantom.exit(1);
    }
}
page.onResourceRequested = function(status){ 
    //console.log('onResourceRequested(' + status + ')');
}
page.onFileDownloadError = function(status){  
    //console.log('onFileDownloadError(' + status + ')');//phantom.exit(1);
}
page.onLoadStarted = function(status){ 
    //console.log('onLoadStarted(' + status + ')');
}
page.onLoadFinished = function(status){ 
    //console.log('onLoadFinished(' + status + ')');
    //document.getElementById('qua').textContent;
}
dpkrai96
  • 93
  • 1
  • 11