2

I'm currently working on a Ionic project. We want to transfer Files using authentication, so I do a request to the server with my session token in the header and then the server returns a piece of JSON code. This JSON contains the file type (.png, .docx, etc) and a base64 string of the file.

When I write this base64 string to a file I get the exact same string in the file. However, if I use the atob() function to decode it, I get a corrupted file. When I open this file in notepad, I can see that more than 99% of the file is the same as the original. Only a few characters are different. Does somebody know a solution to this?

The File is uploaded via a web portal to the PHP framework. When we use the PHP code to get the file and decode the base64 string to a file, it works just fine. (PHP framework returns in UTF-8).

$http({method: "GET", url: "PATH TO SERVER" + students[y].id + ".json"}).then (function(result) {
    $cordovaFile.writeFile(cordova.file.externalCacheDirectory, fileName + result.data.data.data(filetype), result.data.data.base64, true).then(function(res) {
        console.log(res);
    });
})
Jur Clerkx
  • 698
  • 4
  • 14
  • Perhaps `atob()` does not properly decode the base64 string, there is a question that discusses this issue [here](https://stackoverflow.com/questions/30106476/using-javascripts-atob-to-decode-base64-doesnt-properly-decode-utf-8-strings). – Dexter May 05 '16 at 17:25

0 Answers0