Javascript Phonegap-如何下载文件
我正在尝试将信息导出/下载到文件中。在我的浏览器中,它工作正常,但一旦我在phonegap应用程序中有了它,它所做的就是以文本形式打开文件,无法保存,然后无法返回应用程序。 有什么建议吗?顺便说一句,我不是js专家——更像是新手Javascript Phonegap-如何下载文件,javascript,cordova,Javascript,Cordova,我正在尝试将信息导出/下载到文件中。在我的浏览器中,它工作正常,但一旦我在phonegap应用程序中有了它,它所做的就是以文本形式打开文件,无法保存,然后无法返回应用程序。 有什么建议吗?顺便说一句,我不是js专家——更像是新手 function dbError(e) { console.log("SQL ERROR"); console.dir(e); } function backup(table) { var def = n
function dbError(e) {
console.log("SQL ERROR");
console.dir(e);
}
function backup(table) {
var def = new $.Deferred();
curatio.webdb.db.readTransaction(function(tx) {
tx.executeSql("select * from "+table, [], function(tx,results) {
var data = convertResults(results);
console.dir(data);
def.resolve(data);
});
}, dbError);
return def;
}
$(document).on("click", "#doBackupBtn", function(e) {
e.preventDefault();
console.log("Begin backup process");
$.when(
backup("allergies")
).then(function(allergies, log) {
console.log("All done");
var data = {allergies:allergies}
var serializedData = JSON.stringify((data), null, 4);
console.log(serializedData);
download("Export.csv", serializedData);
(function(console){
console.save = function(data, filename){
if(!data) {
console.error('Console.save: No data')
return;
}
if(!filename) filename = 'console.json'
if(typeof data === "object"){
data = JSON.stringify(data)
}
var blob = new Blob([data], {type: 'text/json'}),
e = document.createEvent('MouseEvents'),
a = document.createElement('a')
a.download = filename
a.href = window.URL.createObjectURL(blob)
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':')
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
a.dispatchEvent(e)
}
})(console)
});
});
function download(filename, content) {
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/csv;charset=utf-8,' + encodeURIComponent(content));
pom.setAttribute('download', filename);
pom.click();
}
//Generic utility
function convertResults(resultset) {
var results = [];
for(var i=0,len=resultset.rows.length;i<len;i++) {
var row = resultset.rows.item(i);
var result = {};
for(var key in row) {
result[key] = row[key];
}
results.push(result);
}
return results;
}
</script>
如果我读对了,您想导出和导入吗?将数据库转换为文件
我在Github上编写了一些代码来实现这一点,我的。在该代码中,您所要做的就是更改SQL查询和属性。它将您的数据库导出到基于的文件。谢谢,看起来像我要找的。