节点Webkit:将数据导出到csv并提示用户保存文件 [英] node webkit: export data to csv and prompt the user to save the file
问题描述
我可以使用节点fs模块在节点webkit上将数据导出到csv.但是现在我想提示用户将文件保存在他/她想要的位置.就像在html情况下的下载弹出窗口一样.我尝试按照文件对话框.有人可以帮我找到解决方法.这是我的代码.
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-up in case of html. I tried doing on the lines of this code and even this file dialog. Could someone help me find a way out of this. Here's my code.
fs.writeFile("export.csv", data, function(err) {
if(err) {
console.log(err);
} else {
$('#export_file').show();
$('#export_file input').on('change', function (event) {
console.log($(this).val());
console.log("The file was saved!");
});
}
});
<div id="export_file" style="display:none">
<input type="file" nwsaveas nwworkingdir="/data/input/" />
</div>
推荐答案
我在这里 http://9ijy.net/blog/view/6
步骤1在您的html文件中,添加如下所示的Input标签块:
Step 1 In your html file, add a Input tag block like below:
<input id="export_file" type="file" nwsaveas style="display:none" nwworkingdir=""/>
第2步,在您的javascript文件中添加一个新函数,如下所示:
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();
}
第3步,使用如下所示的saveFile(name,data)函数将文件保存到任意位置:
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);
这篇关于节点Webkit:将数据导出到csv并提示用户保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!