Download img/png mime type , casperJS

2019-08-09 01:56发布

问题:

I'm having a casperJS script to trigger an image download on a button click. I have clicked the button, and the resouce.recived has a file of type image/png. How do I download that and save ? When I tried download(resource, "something.png); I get to download HTML(source code of the page) and not the desired image. Relevant segment of my code :

var dFile = casper.on('page.resource.received', function(resource) {
    if (resource.stage !== "end") {
        return;
    }
    if(resource.contentType.indexOf('image') >= 0)
    {
        console.log(resource.status);
        console.log(resource.contentType);
    }
    console.log("Downloading");
    casper.download(resource, "test011.png");

    dFile = resource;
    return dFile;
});

I've had a look at this, downloading a file that comes as an attachment in a POST request response in PhantomJs and tried implementing the solution, but doesn't seem to work. I'm new to Casper and JavaScript as well. Please let me know how to go about this. Thanks !