1

In a normal browser, if i click the link, the browser will open a dialog to save/open the file.

How do i automate this thing using casperjs. Below is the html code

<li>
    <a id="rmdExport" href="#">Export markdown file</a>
</li>

after clicking on it, the browser window opens i tried with the following code but didn't worked

casper.then(function(){
    functions.open_advanceddiv(casper);
    casper.setFilter("page.prompt", function (msg, currentValue) {
        if (msg === "You have choosen to open:") {
            return true;
        }
    });
    this.click("#rmdExport");
});

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

casper.then(function (){
    Notebook = this.fetchText(".jqtree-selected > div:nth-child(1) > span:nth-child(1)");
    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 Notebook+".md"; };
});

I gone through these links How i can save/download link with blob: ?, casperjs download csv file, downloading a file that comes as an attachment in a POST request response in PhantomJs

I know its duplicate question of all these. But i am helpless please help me with this

Community
  • 1
  • 1
Patrick
  • 53
  • 11
  • 1
    It's not an answer, ofc, but if you at all can, [use SlimerJS with CasperJS](https://slimerjs.org/faq.html#launch-casperjs) instead of PhantomJS, here's how to [download files](http://stackoverflow.com/a/35434511/2715393) with it. – Vaviloff Dec 01 '16 at 09:11
  • @Vaviloff, iam using casperjs with slimerjs – Patrick Dec 01 '16 at 09:38
  • 1
    You also need to go to URL where your file is located, if I remember it right. – Vaviloff Dec 01 '16 at 10:57
  • @Vaviloff, could you tell me how to find it? here `href="#"` its not showing any URL here – Patrick Dec 01 '16 at 11:45
  • 1
    Try catching it with `casper.on('resource.requested', function(requestData, resource) { console.log(decodeURI(requestData.url)); });` after the click. – Vaviloff Dec 01 '16 at 12:03
  • @Patrick if you need to avoid the standard dialog `Open / Save this file?` you have to edit `mimeTypes.rdf` on linux `~/.mozilla/firefox/*.default/mimeTypes.rdf` see also: [SuperUser.com Issue](http://superuser.com/questions/848756/disable-open-with-option-on-firefox-downloads-force-firefox-to-always-save-al#850055) –  Dec 02 '16 at 12:28

0 Answers0