我已经使用cordovaFileTransfer插件下载了csv文件。但是文件在我的设备中不可见。如何访问下载的文件。
代码:-
var stockData = [ { 红色”, 值:“#f00” }, { 颜色:“绿色”, 值:“#0f0” }, { 颜色:“蓝色”, 值:“#00f” }, { 颜色:“青色”, 值:“#0ff” }, { 颜色:“洋红色”, 值:“#f0f” }, { 颜色:“黄色”, 值:“#ff0” }, { 颜色:“黑色”, 值:“#000” } ];
function convertArrayOfObjectsToCSV(args) {
var result, ctr, keys, columnDelimiter, lineDelimiter, data;
data = args.data || null;
if (data == null || !data.length) {
return null;
}
columnDelimiter = args.columnDelimiter || ',';
lineDelimiter = args.lineDelimiter || '\n';
keys = Object.keys(data[0]);
result = '';
result += keys.join(columnDelimiter);
result += lineDelimiter;
data.forEach(function(item) {
ctr = 0;
keys.forEach(function(key) {
if (ctr > 0) result += columnDelimiter;
result += item[key];
ctr++;
});
result += lineDelimiter;
});
return result;
}
$scope.downloadCSV=function(args) {
//alert(JSON.stringify(args))
var data, filename, link;
var csv = convertArrayOfObjectsToCSV({
data: stockData
});
if (csv == null) return;
//filename = args.filename || 'export.csv';
if (!csv.match(/^data:text\/csv/i)) {
// csv = 'data:text/csv;charset=utf-8,' + csv;
csv = csv;
}
//data = encodeURI(csv);
//link = document.createElement('a');
//link.setAttribute('href', data);
//link.setAttribute('download', filename);
//link.click();
ionic.Platform.ready(function(){
var scc=cordova.file.dataDirectory;
//alert(scc);
$cordovaFile.writeFile(scc,'sk12.csv', csv, true).then(function(result){
alert('Success! Export created!'+JSON.stringify(result));
var url = result.target.localURL;
var filename = url.split("/").pop();
var targetPath2 = cordova.file.dataDirectory + filename;
$cordovaFileTransfer.download(url, targetPath2, {}, true).then(function (result) {
$scope.hasil = 'Save file on '+targetPath2+' success!';
$scope.mywallpaper=targetPath2;
}, function (error) {
$scope.hasil = 'Error Download file';
}, function (progress) {
$scope.downloadProgress = (progress.loaded / progress.total) * 100;
// alert("dom "+$scope.downloadProgress);
});
}, function(err) {
alert("ERROR"+JSON.stringify(err));
});
}) }