0

So I was testing my code to see if my uploaded image was converted to base64 successfully. When I did alert(data) however I found that it was a garbled mess of unknown characters. I wanted to have the popup message contain the base64 string so I could put it into an online base64 decoder to confirm that the uploaded image was correct.

Afterwards I want to give the file name + base64 string to my php script which then writes it to the server.

However this is what I got

https://i.stack.imgur.com/089Gq.png

Here is the code that is supposed to take the given image and convert it.

var app = angular.module("app", ["ui.bootstrap"]);

//http://stackoverflow.com/questions/18571001/file-upload-using-angularjs
app.factory('API', function ($http) {   
    return {
    uploadImage: function (image) {
        return $http.post('/js/upload.php', image);
    }
    }
});

app.controller('MainController',['$scope', '$http', 'API', function($scope, $http, API) {
    $scope.imageUrl = "";
    $scope.template = "";
    $scope.templates = [];

    $scope.templates.push("select an option...");
    $scope.templates.push("MakeGray");
    $scope.templates.push("Canny");
    $scope.templates.push("HSV");

    $scope.template = $scope.templates[0];

    $scope.add = function() {

    var f = document.getElementById('fileToUpload').files[0];  // name of image
    var files = document.getElementById('fileToUpload').files;
    var r = new FileReader();

    r.onload = function(event){
        console.log(event.target.result);
    }

    r.onloadend = function(e) {

        var data = e.target.result;
        alert(data);

        var formData = new FormData();

        formData.append("fileToUpload", f,f.name);

        API.uploadImage(formData)
        .success(function (imgUrl) {
            $scope.imageUrl = imgUrl;
        })
        .error (function (error) {
        });
    }

    r.readAsBinaryString(f);
    }
}]);
Zypps987
  • 404
  • 6
  • 21

1 Answers1

2

Reading as BinaryString is sure to give you unprintable characters. If you want it as base64 encoded (specifically to display as image) then you need to read it as DataURL. Following will definitely help you:

r.readAsDataURL(f);

For more information on FileReader consult MDN docs.

Here is working fiddle to play with different read types.

TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
  • Thank you for response, I will try this out and respond back. Are you an angularJS developer? – Zypps987 Dec 21 '15 at 11:24
  • I tried to use $("#img1").prop("src",data); in my angularJS but it was giving the following error. ReferenceError: $ is not defined $("#img1").prop("src", data); Also, how are you getting away with checking if v is equal to something on line 12 in the JS if you haven't defined v before hand? – Zypps987 Dec 21 '15 at 12:15
  • well, yes I'm working on and learning angular js. now about your second comment. 1. I don't think this is the angular way of doing it. If you set `imageUrl` to value read from file (that is `event.target.result`) it should reflect. 2. `v` is already defined before the `onloandend` is called. Someone can explain it by using closure/variable scoping etc etc... – TheVillageIdiot Dec 21 '15 at 12:24
  • Ok nvm I was being dumb and forgot that I needed to have jQuery in my html sources. Thank you so much for your help, this problem is something I've been working on for a while. Also, is there a way to scale the image to a max height / width? Like it must always be within a 800x800 border? – Zypps987 Dec 21 '15 at 12:29
  • I've updated fiddle to set height/width to 250px using css class for img element. you can try that. – TheVillageIdiot Dec 21 '15 at 12:36
  • Wow, I love you. You solved a problem in 5 minutes that a weeks worth of asking on stackoverflow couldn't do. Thanks a bunch! Is there a way to contact you in the future if I need help? – Zypps987 Dec 21 '15 at 12:38
  • ha ha I think mentioning using @ helps :) Glad I was able to help! – TheVillageIdiot Dec 21 '15 at 12:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98572/discussion-between-zypps987-and-thevillageidiot). – Zypps987 Dec 21 '15 at 13:50
  • I was able to find a way to make the 2 images side by side, but any idea of how to make them a little lower on the page? Screenshot of website imgur.com/jS1vEKq Here is the html pastebin.com/ZZt4MBPY – Zypps987 Dec 21 '15 at 14:18