Skip to content

Commit

Permalink
Added export download for browser aswell
Browse files Browse the repository at this point in the history
  • Loading branch information
graphefruit committed Jan 15, 2018
1 parent ea468fb commit 30730d2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 33 deletions.
5 changes: 4 additions & 1 deletion src/pages/brews/brews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ export class BrewsPage {
csvFile += processRow(rows[i]);
}

me.uiHelper.exportCSV(filename, csvFile);
me.uiHelper.exportCSV(filename, csvFile).then(() => {
alert('Datei wurde erfolgreich heruntergeladen');
});

}

Expand All @@ -151,6 +153,7 @@ export class BrewsPage {
{"VALUE": brew.coffee_concentration, "LABEL": "Kaffee-Konzentration"},
{"VALUE": brew.coffee_first_drip_time, "LABEL": "Erster Kaffeetropfen"},
{"VALUE": brew.coffee_blooming_time, "LABEL": "Blooming-Zeit / Preinfusion"},
{"VALUE":brew.getCalculatedBeanAge(),"LABEL":"Bohnenalter"},
];
entries.push(entry);
}
Expand Down
90 changes: 58 additions & 32 deletions src/services/uiHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,54 +91,80 @@ export class UIHelper {
}

public exportCSV(fileName: string, csvContent: string) {
let errorCallback = (e) => {
console.log("Error: " + e);
};

let storageLocation: string = "";
var blob = new Blob([csvContent], {type: 'text/csv;charset=UTF-8'});
switch (device.platform) {
let promise = new Promise((resolve, reject) => {
let errorCallback = (e) => {
console.log("Error: " + e);
reject();
};
var blob = new Blob([csvContent], {type: 'text/csv;charset=UTF-8'});
if (this.platform.is("android")) {
let storageLocation: string = "";

case "Android":
storageLocation = 'file:///storage/emulated/0/';
break;
case "iOS":
storageLocation = cordova.file.documentsDirectory;
break;
switch (device.platform) {

}
case "Android":
storageLocation = 'file:///storage/emulated/0/';
break;
case "iOS":
storageLocation = cordova.file.documentsDirectory;
break;

window.resolveLocalFileSystemURL(storageLocation,
function (fileSystem) {
}

fileSystem.getDirectory('Download', {
create: true,
exclusive: false
},
function (directory) {
window.resolveLocalFileSystemURL(storageLocation,
function (fileSystem) {

//You need to put the name you would like to use for the file here.
directory.getFile(fileName, {
fileSystem.getDirectory('Download', {
create: true,
exclusive: false
},
function (fileEntry) {
function (directory) {

//You need to put the name you would like to use for the file here.
directory.getFile(fileName, {
create: true,
exclusive: false
},
function (fileEntry) {


fileEntry.createWriter(function (writer) {
writer.onwriteend = function () {
console.log("File written to downloads")
};
fileEntry.createWriter(function (writer) {
writer.onwriteend = function () {
resolve();

writer.seek(0);
writer.write(blob); //You need to put the file, blob or base64 representation here.
};

}, errorCallback);
writer.seek(0);
writer.write(blob); //You need to put the file, blob or base64 representation here.

}, errorCallback);
}, errorCallback);
}, errorCallback);
}, errorCallback);
}, errorCallback);

}
else {
setTimeout(() => {
if (navigator.msSaveBlob) { // IE 10+
navigator.msSaveBlob(blob, fileName);
} else {
var link = document.createElement("a");
if (link.download !== undefined) { // feature detection
// Browsers that support HTML5 download attribute
var url = URL.createObjectURL(blob);
link.setAttribute("href", url);
link.setAttribute("download", fileName);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);

}
}
}, 250);
}

});
return promise;
}


Expand Down

0 comments on commit 30730d2

Please sign in to comment.