3

I have written a script that clicks on a link which can download a mp3 file. The problem I am facing is when the script simulates the click on that link, a download dialog box pops up like this:
Download Dialog Box

Now, I want to save this file to some path of my choice and automate this whole process. I am clueless on how to handle this dialog box.

Akshay Bhasin
  • 167
  • 2
  • 3
  • 11
  • How do you run SlimerJS? Lite or Full version? What operating system? – Vaviloff Feb 16 '16 at 16:51
  • what exactly do you mean by lite or full version? Sorry I am just a beginner. Operating System is windows 8.1. – Akshay Bhasin Feb 16 '16 at 18:06
  • Does SlimerJS use existing Firefox installation? – Vaviloff Feb 17 '16 at 01:53
  • yes i think it does..But I tried opening the file with media player and it did played after a while. It was saving the file in Appdata/local/temp folder. Any idea how I can change that path? – Akshay Bhasin Feb 17 '16 at 08:30
  • I tried to manually launch Firefox which is driven by SlimerJS and change its settings so that 1) it would automatically save files to a predefined folder and 2) for mp3 files to be saveв automatically (Tools » Options » Applications » Search for "mp3", set Action to "Save File". But those settings don't seem to work when Firefox is launched from SlimerJS. – Vaviloff Feb 17 '16 at 12:10

3 Answers3

0

Here's a script adapted from this blog post to download a file.

In SlimerJS it is possible to use response.body inside the onResourceReceived handler. However to prevent using too much memory it does not get anything by default. You have to first set page.captureContent to say what you want. You assign an array of regexes to page.captureContent to say which files to receive. The regex is applied to the mime-type. In the example code below I use /.*/ to mean "get everything". Using [/^image/.+$/] should just get images, etc.

var fs=require('fs');
var page = require('webpage').create();

fs.makeTree('contents');

page.captureContent = [ /.*/ ];

page.onResourceReceived = function(response) {

    if(response.stage!="end" || !response.bodySize)
    {
        return;
    }

    var matches = response.url.match(/[/]([^/]+)$/);
    var fname = "contents/"+matches[1];

    console.log("Saving "+response.bodySize+" bytes to "+fname);
    fs.write(fname,response.body);

    phantom.exit();
};

page.onResourceRequested = function(requestData, networkRequest) {
    //console.log('Request (#' + requestData.id + '): ' + JSON.stringify(requestData));
};

page.open("http://....mp3", function(){

});
Vaviloff
  • 16,282
  • 6
  • 48
  • 56
  • Hi @Vaviloff thanks for the suggestion. I tried this code but what it does is that it grabs all htmls and js and images of the website but still that download box is popping up. Is there any way I can choose some options by default? – Akshay Bhasin Feb 16 '16 at 16:27
0

You can't control a dialog box. SlimerJS doesn't have API for this action.

Vaviloff
  • 16,282
  • 6
  • 48
  • 56
Artem Chernov
  • 856
  • 2
  • 8
  • 26
0

Firefox generates a temp "downloadfile.extension.part" file which contains the content. Just simply rename the file ex. myfile.csv.part > myfile.csv

locally if working on a mac you should find the .part file in the downloads directory, on linux /temp/ folder

Not the most elegant solution but should do the trick

KornholioBeavis
  • 2,402
  • 2
  • 19
  • 26