我已使用Cordova文件传输插件在设备上下载了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) {
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;
}
ionic.Platform.ready(function(){
var scc=cordova.file.dataDirectory;
$cordovaFile.writeFile(scc,'sk.csv', csv, true).then(function(result){
alert('Success! Export created!'+JSON.stringify(result));
//alert(JSON.stringify(result.target.localURL));
var url = result.target.localURL;
//alert(url);
var filename = url.split("/").pop();
var targetPath2 = cordova.file.documentsDirectory + filename;
//alert(targetPath2);
$cordovaFileTransfer.download(url, targetPath2, {}, true).then(function (result) {
$ionicLoading.hide();
// alert(JSON.stringify(result));
$scope.hasil = 'Save file on '+targetPath2+' success!';
$scope.mywallpaper=targetPath2;
alert( $scope.hasil);
//alert($scope.mywallpaper);
}, 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));
});
$cordovaFile.readAsText(scc, 'sk.csv')
.then(function (success) {
alert("sc "+JSON.stringify(success));
}, function (error) {
alert("ERROR"+JSON.stringify(error));
});
如何解决此问题。