casperjs下载文件时未指定url [英] casperjs download file without specifying url

查看:92
本文介绍了casperjs下载文件时未指定url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过Casperjs下载CSV文件而不指定下载URL?我正在尝试下载CSV文件,该文件的URL是在单击下载按钮时动态生成的。因此,在这种情况下,我可能无法很好地使用download()。

Is there any way to download CSV file with casperjs without specifying download URL? I am trying to download CSV file whose URL is dynamically generated when I click the download button. So, I may not be able to use download() well under the situation.

推荐答案

记录下来,已经可以使用 resource.received事件了。
如果您收到像这样的标题:

For the record, it's already possible using 'resource.received' event. If you receive header like this one:


Content-Disposition:Attachment; Filename = ExportData.csv

Content-Disposition: Attachment; Filename="ExportData.csv"

可以使用以下事件侦听器下载生成的文件:

The file generated can be downloaded using following event listener:

casper.on('resource.received', function(resource) {
    if (resource.stage !== "end") {
        console.log("resource.stage !== 'end'");
        return;
    }
    if (resource.url.indexOf('ExportData.csv') > -1) {
        console.log("Downloading csv file");
        this.download(resource.url, 'ExportData.csv');
    }
});

这篇关于casperjs下载文件时未指定url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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