在PhantomJs中下载作为POST请求响应附件的文件 [英] downloading a file that comes as an attachment in a POST request response in PhantomJs

查看:114
本文介绍了在PhantomJs中下载作为POST请求响应附件的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下载一个CSV文件,它是通过单击POST请求中的按钮而生成的.我在casperJs和phantomJS论坛上进行了最大程度的研究,然后空手而归.在像firefox这样的普通浏览器中,发布请求后会出现一个浏览器下载对话框窗口.如何在PhantomJS中处理这种情况

I want to download a CSV file, it is generated on a button click through a POST request. I researched to my best on casperJs and phantomJS forums and returned empty handed. In a normal browser like firefox, a browser download dialog window appears after the post request. How to handle this case in PhantomJS

TTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
Content-disposition: attachment;filename=ExportData.csv
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Fri, 19 Apr 2013 23:26:40 GMT
Content-Length: 65183

推荐答案

我已经找到一种使用casperjs的方法(如果使用XMLHttpRequest实现下载功能,它应该仅与phantomjs一起使用,但是我没有尝试过).

I've found a way to do this using casperjs (it should work with phantomjs alone if you implement the download function using XMLHttpRequest, but i've not tried).

我将为您提供一个工作示例,该示例尝试从此页面.当您单击下载链接时,将触发一些javascript代码,这些代码将生成一些隐藏的输入字段,然后将它们过帐.

I'll leave you the working example, that tries to download the mos recent PDF from this page. When you click the download link, some javascript code is triggered that generates some hidden input fields that are then POSTed.

我们要做的是替换表单的onsubmit函数,以便取消提交,并获取表单目标(操作)及其所有字段.我们稍后将使用此信息进行实际下载.

What we do is replace the form's onsubmit function so that it cancels the submission, and get the form destination (action) and all its fields. We use this information later to do the actual download.

var casper=require('casper').create();
casper.start("https://sede.gobcan.es/tributos/jsf/publico/notificaciones/comparecencia/ultimosanuncios.jsp", function() {

    var theFormRequest = this.page.evaluate(function() {
        var request = {}; 
        var formDom = document.forms["resultadoUltimasNotif"];
        formDom.onsubmit = function() {
            //iterate the form fields
            var data = {};
            for(var i = 0; i < formDom.elements.length; i++) {
               data[formDom.elements[i].name] = formDom.elements[i].value;
            }
            request.action = formDom.action;
            request.data = data;
            return false; //Stop form submission
        }

        //Trigger the click on the link.
        var link = $("table.listado tbody tr:first a");
        link.click();

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

    //Start the download
    casper.download(theFormRequest.action, "downloaded_file.pdf", "POST", theFormRequest.data);
});

casper.run(); 

注意:您必须使用--ignore-ssl-errors运行它,因为它们使用的CA不在您的浏览器默认CA列表中.

Note: you have to run it with --ignore-ssl-errors, as the CA they use isn't in your browser default CA list.

casperjs --ignore-ssl-errors=true downloadscript.js

这篇关于在PhantomJs中下载作为POST请求响应附件的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆