ionic save base64 as file

Solutions on MaxInterview for ionic save base64 as file by the best coders in the world

showing results for - "ionic save base64 as file"
Angelo
03 Jul 2019
1let UUID = 'empleado-' + (new Date().getTime()).toString(16);
2
3// imagen = data:image/jpeg;base64,/9j/4........
4    let realData = imagen.split(",")[1];
5    let blob = this.b64toBlob(realData, 'image/jpeg');
6
7    this.file.checkDir(this.file.externalApplicationStorageDirectory, 'DirectorioFotos')
8        .then(_ => {
9          this.file.writeFile(this.file.externalApplicationStorageDirectory + 'DirectorioFotos/', UUID + '.jpg', blob).then(response => {
10            // ACTION
11          }).catch(err => {
12            // ACTION
13          })
14        })
15        .catch(err => {
16          this.file.createDir(this.file.externalApplicationStorageDirectory, 'DirectorioFotos', false).then(result => {
17            this.file.writeFile(this.file.externalApplicationStorageDirectory + 'DirectorioFotos/', UUID + '.jpg', blob).then(response => {
18              // ACTION
19            }).catch(err => {
20              // ACTION
21            })
22          })
23        });
24 
25 //convert base64 to blob
26 b64toBlob(b64Data, contentType) {
27    contentType = contentType || '';
28    var sliceSize = 512;
29    var byteCharacters = atob(b64Data);
30    var byteArrays = [];
31
32    for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
33      var slice = byteCharacters.slice(offset, offset + sliceSize);
34
35      var byteNumbers = new Array(slice.length);
36      for (var i = 0; i < slice.length; i++) {
37        byteNumbers[i] = slice.charCodeAt(i);
38      }
39
40      var byteArray = new Uint8Array(byteNumbers);
41
42      byteArrays.push(byteArray);
43    }
44
45    var blob = new Blob(byteArrays, {type: contentType});
46    return blob;
47  }