在本地将 WebSQL 导出为 CSV
我想使用 webkit 浏览器将 WebSQL 数据库导出到本地移动设备上的 csv 文件中。目前,此代码在 google chrome 中运行,并创建一个自动下载的文件:
testCSV = function(){
var csvData = "";
db.transaction(function(tx){
tx.executeSql('SELECT * FROM grocery', [], function (tx, results){
var len = results.rows.length, i;
for (i = 1; i < len; i++) {
csvData += results.rows.item(i).itemno + "," + results.rows.item(i).quantity + "\n";
}
window.location='data:text/csv;charset=utf8,' + encodeURIComponent(csvData);
});
});
};
当我在移动设备上运行相同的代码时,它会打开并在浏览器中显示 csv 文本。我怎样才能下载文件并将其保存在本地。有什么想法吗?
I would like to export a WebSQL database into a csv file locally on a mobile device using a webkit browser. Currently this code works inside google chrome and creates a file that automatically downloads:
testCSV = function(){
var csvData = "";
db.transaction(function(tx){
tx.executeSql('SELECT * FROM grocery', [], function (tx, results){
var len = results.rows.length, i;
for (i = 1; i < len; i++) {
csvData += results.rows.item(i).itemno + "," + results.rows.item(i).quantity + "\n";
}
window.location='data:text/csv;charset=utf8,' + encodeURIComponent(csvData);
});
});
};
When I run the same code on the mobile device it opens and displays the csv text in the browser. How can I get it to download the file and save it locally. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
text/csv
更改为自定义 MIME 类型,例如application/webcsv
触发未知文件类型的下载提示。Change
text/csv
to a custom MIME type such asapplication/webcsv
to trigger the download prompt for an unknown file type.