Node Webkit: Export Data To Csv And Prompt The User To Save The File
I could implement the export data to csv on node webkit with node fs modules. But now I want to prompt the user to save the file in his/her desired location. Like the download pop-
Solution 1:
I found a solution here http://9ijy.net/blog/view/6
Step 1 In your html file, add a Input tag block like below:
<inputid="export_file"type="file" nwsaveas style="display:none" nwworkingdir=""/>
Step 2 Add a new function in your javascript file like below:
functionsaveFile(name,data) {
var chooser = document.querySelector(name);
chooser.addEventListener("change", function(evt) {
console.log(this.value); // get your file namevar 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);
Solution 2:
- You are writing the file dialog code as a callback to fs.writeFile. A callback means it will be called AFTER the file is saved. This is what might be happening right now. 1) File is written in the current directory where node-webkit app is running from. 2) When the file is saved without any errors, the dialog shows up. But this dialog is of no help.
- Instead, show the dialog earlier and get the file-path from it(https://github.com/rogerwang/node-webkit/wiki/File-dialogs). Pass this path as the first arg in fs.writeFile(). Done.
Post a Comment for "Node Webkit: Export Data To Csv And Prompt The User To Save The File"