3

I convert kml file to base 64. Now I want to encode base 64 to become kml file again? is this possible? I convert kml file like this..

        $scope.myFunction = function () {
        var files = document.getElementById('myFile').files;
        if (files.length > 0) {
            getBase64(files[0]);
        }
    }

    function getBase64(file) {
        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function () {
            console.log(reader.result);
        };
        reader.onerror = function (error) {
            console.log('Error: ', error);
        };
    }

how can I store kml file to database?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Yamoshi Wolverine
  • 401
  • 1
  • 5
  • 12
  • Possible duplicate of [Creating a Blob from a base64 string in JavaScript](https://stackoverflow.com/questions/16245767/creating-a-blob-from-a-base64-string-in-javascript) – Endless Aug 27 '17 at 17:33
  • why do you event want to convert it to base64? – Endless Aug 27 '17 at 17:33

1 Answers1

1

It surely is possible, you just have to decode the base64 and turn the result into a file again.

function base64ToFile(base64){

  content=atob(base64);
  var file = new Blob([content], {type: 'kml'});

  //You can now asign the file to a link to download, send it with ajax, etc..

  a.href = URL.createObjectURL(file);

}

Bear in mind, that you cant write a file directly onto disk from javascript, because that would be a security issue, you need t