question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to convert to base 64 and DATA URI.

See original GitHub issue

controller $scope.PhotoData = []; $scope.choosePhotos = function () { var options = { maximumImagesCount: 10, quality: 80, targetWidth: 1000, targetHeight: 1000, };

$cordovaImagePicker.getPictures(options).then(function (results) {
for (var i = 0; i < results.length; i++) {
$scope.PhotoData.push(results[i]);
console.log(results[i]);
        }
    if(!$scope.$$phase) {
      $scope.$apply();
      }
        }, function (err) {
                    // An error occured. Show a message to the user
                });
                };

view code ion-slide ng-repeat=“item in PhotoData” img ng-src=“data:image/jpg;base64,{{item}}” style=“max-width: 100%”

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
tomercagancommented, Jun 1, 2016

You can use FileReader:

function processImage(file) {
    fileReader = new FileReader();
    fileReader.onload = onload;
    fileReader.onprogress = progress;
    resolveFile(file).then(function(actualFile) {
        fileReader.readAsBinaryString(actualFile);
    });
}

var resolveFile = function(entry) {
    var deferred = $q.defer();
    // first convert to local file system URL
    window.resolveLocalFileSystemURL(entry, function(fileEntry) {
        // now read/convert the file to file object.
        fileEntry.file(function(file) {
            console.log("File converted to file entry");
            deferred.resolve(file);
        }, function(err) {
            console.log("Failed to convert to file entry", err);
            deferred.reject(err);
        });
    }, function(err) {
        console.log("Failed to resolve to file URL", err);
        deferred.reject(err);
    });

    return deferred.promise;
};

var onload = function (evt) {
    var encoded = window.btoa(evt.target.result);
    // now encoded contains the base64 image.
    // note it is without the "header"
    // img.src = "data:image/jpeg;base64," + encoded
};

Initially I tried to use fileReader.readAsDataURL(actualFile); which give you the base64 data in one shot but it resulted in corrupt images. A workaround was suggested with readAsBinaryString() and then window.btoa() and solved it. See SO question here.

0reactions
urbanmaniacommented, May 26, 2016

yes, this works, but the problem with is it you take for example a compressed jpeg and convert it to an uncompressed png, resulting in a double the size of the file or more.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Convert a Data URI to Base64 or 'File Contents' in Power ...
Discover how to convert a Data URI to a base64 string or Power Automate 'File Contents' property using expressions in Microsoft Power ...
Read more >
How to convert URL to Base64
Type or paste paste the URL-address · Press the “Encode URL to Base64” button. · Download or copy the result from the “Base64”...
Read more >
Data URLs - HTTP - MDN Web Docs - Mozilla
Base64 is a group of binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 ......
Read more >
Base64 Image Encoder
Optimize your images and convert them to base64 online. Drag & Drop your files, copy to clipboard with a click and use the...
Read more >
How to convert base64 to normal image URL? - Stack Overflow
Your function is correct, but after it you have to use URL.createObjectURL() like follows: function dataURItoBlob(dataURI) { // convert ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found