Skip to content

Commit 8567df2

Browse files
committed
fix: save method
1 parent d7dbfe7 commit 8567df2

1 file changed

Lines changed: 26 additions & 4 deletions

File tree

src/app/afip/afip.component.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import { HttpClient } from '@angular/common/http';
88
import { Filesystem, Directory, Encoding } from '@capacitor/filesystem';
99
import { FileOpener } from '@ionic-native/file-opener/ngx';
1010

11-
import * as XLSX from 'xlsx';
12-
1311
export interface InvoiceElement {
1412
docName: string;
1513
docNumber: string;
@@ -206,12 +204,36 @@ export class AfipComponent implements OnInit {
206204
let month = `${(date.getMonth()+1)}`.padStart(2,'0');
207205
let year = date.getFullYear();
208206

209-
const result = await Filesystem.writeFile({
207+
// Method for save in Documents only
208+
/*const result = await Filesystem.writeFile({
210209
path: 'invoices_afip_' + `${year}${month}${day}` + '.csv',
211210
data: csvContent,
212211
directory: Directory.Documents,
213212
encoding: Encoding.UTF8,
214-
});
213+
});*/
214+
215+
// Save and Open the file with Data (better option in android actually)
216+
const blob = new Blob([csvContent], { type: "text/csv" });
217+
var reader = new FileReader();
218+
reader.readAsDataURL(blob);
219+
reader.onloadend = async function () {
220+
var base64data = reader.result;
221+
try {
222+
const result = await Filesystem.writeFile({
223+
path: 'invoices_afip_' + `${year}${month}${day}` + '.csv',
224+
data: <string>base64data,
225+
directory: Directory.Data,
226+
recursive: true
227+
});
228+
let fileOpener: FileOpener = new FileOpener();
229+
fileOpener.open(result.uri, blob.type)
230+
.then(() => console.log('File open'))
231+
.catch(e => console.log('Error to open file', e));
232+
console.log('File write', result.uri);
233+
} catch (e) {
234+
console.error('Error to write file', e);
235+
}
236+
}
215237
}
216238

217239
openDialog(msg: string) {

0 commit comments

Comments
 (0)