【问题标题】:casperjs download file without specifying urlcasperjs下载文件而不指定url
【发布时间】:2017-09-06 23:50:51
【问题描述】:

有没有办法在不指定下载 URL 的情况下使用 casperjs 下载 CSV 文件?我正在尝试下载单击下载按钮时动态生成其 URL 的 CSV 文件。因此,在这种情况下,我可能无法很好地使用 download()。

【问题讨论】:

    标签: casperjs


    【解决方案1】:

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

    Content-Disposition:附件;文件名="ExportData.csv"

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

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

    【讨论】:

    【解决方案2】:

    我已经找到了解决方案。它不是很干净,但很有效:

    您需要构建一个全局数组,它将每个资源相关联。received 具有附加的文件名。

    fileInfos[url]= parse_filename_from_responseHeader(resource)
    

    如果url是你要下载的资源,先试试open(url)。这将触发 resiyrce.received 事件,解析头部并更新全局数组。

    现在,在启动 casper.download(url) 之前,请查找 fileInfos[url]

    您将在 fileInfos 数组中找到与 url 对应的文件名。 如果需要,我可以详细说明解决方案,但由于这个问题已经存在几年了,

    我会等待下一次戳详细说明。

    【讨论】:

    • 请详细说明代码,想检查URL是否是使用casperjs下载的URL。
    【解决方案3】:

    在解决这个 phantomjs 问题之前,这是不可能的http://code.google.com/p/phantomjs/issues/detail?id=52

    【讨论】:

    • 我知道你没有时间,你正在寻找一个团队继续做项目(兼容 PhantomJS v2.0)。你知道最后期限吗?
    【解决方案4】:

    我会做这样的事情,但我知道我将收到的文件名:

    this.waitForResource(function check(resource){
                res = resource;    
                // regular expression to test
                return /thefilename/.test(resource.url);
            }, function(){
                this.echo("Resource found: " + res.url);
                // download the file
                this.download(res.url, path);
            }, null, 10000);
    

    【讨论】:

      猜你喜欢
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-09
      • 1970-01-01
      • 2016-02-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多