如何使用Phantom js和casper js中的无头浏览器或任何其他选项下载Content-Disposition附件

时间:2014-08-25 12:28:23

标签: jquery phantomjs casperjs

Receive {
    "contentType": "text/plain",
    "headers": [
        {
            "name": "Cache-Control",
            "value": "private"
        },
        {
            "name": "Content-Length",
            "value": "256"
        },
        {
            "name": "Content-Type",
            "value": "text/plain"
        },
        {
            "name": "Server",
            "value": "Microsoft-IIS/7.0"
        },
        {
            "name": "X-AspNet-Version",
            "value": "4.0.30319"
        },
        {
            "name": "Content-Disposition",
            "value": "attachment; filename=\"ContactList_08-25-14.csv\""
        },
        {
            "name": "X-Powered-By",
            "value": "ASP.NET"
        },
        {
            "name": "p3p",
            "value": "policyref=\"/w3c/p3p.xml\", CP=\"COM CNT DEM FIN GOV INT NAV ONL PHY PRE PUR STA UNI IDC CAO OTI DSP COR CUR OUR IND\""
        },
        {
            "name": "Date",
            "value": "Mon, 25 Aug 2014 12:19:25 GMT"
        }
    ],
    "id": 104,
    "redirectURL": null,
    "stage": "end",
    "status": 200,
    "statusText": "OK",
    "time": "2014-08-25T12:19:30.605Z",
    "url": "http://xxxxxxxxxx.com/xxxxxxxx/ExportSubscribers.aspx"
}

3 个答案:

答案 0 :(得分:0)

这个答案中没有代码,抱歉。

但是你不需要无头浏览器,你需要的只是原始响应体。所以只需读取Content-Length(256)字节的响应正文并将它们放在某处。它们应包含实际的文件内容。

此方法适用于任何内容类型,在您的示例中为text/plain但有时可能为application/...,然后响应正文将包含二进制数据。

作为旁注,HTML页面为text/html,并且不会作为附件发送。

此外,由于您已将这些响应标头转换为数组,因此您可以从服务器检索建议的文件名以将其保存在本地。

答案 1 :(得分:0)

我已经成功了。 有一个测试版的phantomjs 2.0,它包含一个解决这个问题的事件处理程序。

它仍然是测试版,所以没有调试。

所以我在发布版本上开发了点击和页面处理,然后更改了幻影版本以使下载工作。

 casper.start('http://www.website.com.br/', function() {
    this.page.onFileDownload = function(status){console.log('onFileDownload(' + status + ')'); 

//SYSTEM WILL DETECT THE DOWNLOAD, BUT YOU WILL HAVE TO NAME THE FILE BY YOURSLEF!!
return "ContactList_08-25-14.csv"; };

    });
      casper.then(function() {
        //DO YOUR STUFF HERE TO CLICK ON THE DOWNLOAD LINK. 
      });
    casper.run();

下载: Phantom 2.0 BETA

下载exe文件,将phantom.exe的发行版重命名为phantom.bkp.exe 并在此处插入此2.0版本。 然后,在casperjs中,你需要在casperjs / bin / bootstrap.js的开头添加一些行

 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 */
var system = require('system');
    var argsdeprecated = system.args;
    argsdeprecated.shift();
    phantom.args = argsdeprecated;

还评论版本检查(同一文件):

(function(version) {
        // required version check
      /*  if (version.major !== 1) {
            return __die('CasperJS needs PhantomJS v1.x');
        } if (version.minor < 8) {
            return __die('CasperJS needs at least PhantomJS v1.8 or later.');
        }
        if (version.minor === 8 && version.patch < 1) {
            return __die('CasperJS needs at least PhantomJS v1.8.1 or later.');
        } */
    })(phantom.version);

请记住,这是一个调整!!

因此,如果要运行幻像发布版本或slimerjs,则引导程序上的这些行会导致问题。

所以开发发布版本,而不是调整到这个版本才能下载。 如果需要调试,则必须删除bootstrap.js

的行

答案 2 :(得分:-1)

我也在尝试基于这篇文章:https://stackoverflow.com/a/23521991/3156756

我没有成功,因为我的表单没有ID,只是一个名字。 但我也试图能够进行这种类型的下载。