Furzel
Furzel

Reputation: 616

node-webkit using window.open() to download a file open another window

I'm currently porting a nodejs / angular webapp to Windows using nodewebkit. Everything has been working pretty well for now but I'm facing a litle problem when I want the user to download a file.

In order to start the download with the save file Dialog, I use a simple window.open(url) where url can be a link to any kind of files. This line actually do it's job and pop the window dialog for saving a file, but at the same time, a blank node-webkit page appears.

I've been trying to mess around with node-webkit for a while without managing to remove this blank page.

As anyone experienced the same kind of behavior ? I'll be interested in any lead, I'm not into the js stuff for long so I may have missed something obviouvs.

Have a nice wathever time of the day it is where you live !

Upvotes: 0

Views: 2809

Answers (2)

user3591588
user3591588

Reputation: 66

a more better solution is found here

Step 1 In your html file, add a Input tag block like below:

<input id="export_file" type="file" nwsaveas style="display:none" nwworkingdir=""/>

Step 2 Add a new function in your javascript file like below:

function saveFile(name,data) {
    var chooser = document.querySelector(name);
    chooser.addEventListener("change", function(evt) {
      console.log(this.value); // get your file name
     var fs = require('fs');// save it now
fs.writeFile(this.value, data, function(err) {
    if(err) {
       alert("error"+err);
    }
});
    }, false);

    chooser.click();  
  }

Step 3 Save your file where ever you like by using saveFile(name,data) function like below:

...

_exportCSV="you data to save";

saveFile('#export_file',_exportCSV);

...

Upvotes: 1

Jack
Jack

Reputation: 3670

As questioner said to be working:

location.href = url

Is the correct usage.

Upvotes: 0

Related Questions