【问题标题】:Casperjs Download Post AttachmentCasperjs 下载帖子附件
【发布时间】:2015-12-08 15:33:06
【问题描述】:

我发现了这个问题并关注了它downloading a file that comes as an attachment in a POST request response in PhantomJs。但是它保存的文件是一个html文件,上面写着404 - File or directory not found.我还有什么东西要下载这个csv文件吗?

这里是响应头

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

这是我的代码

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);
            });
    });

【问题讨论】:

  • 明确一点:res.action 中有什么? casper.download() 需要一个 URL 作为它的第一个参数。

标签: javascript casperjs


【解决方案1】:

我最终使用Selenium 来下载文件,因为看起来该网站已经实施了某种安全措施,不允许我在原始发布请求之外下载它

【讨论】:

    【解决方案2】:

    使用 phantomjs 下载任何报告文件。我正在使用phantomjs 2.0 for windowsRun 下面的命令 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-19
      • 1970-01-01
      • 2014-05-25
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      相关资源
      最近更新 更多