Null Pointer Null Pointer - 1 month ago 15
AngularJS Question

Image not saving properly in blob field (Angular App to Python)

I'm trying to save image from

angular App
to
mysql
blob field in
python
but Image not saving properly.

This is how I'm converting image to byte-array

var reader = new FileReader();
reader.readAsArrayBuffer($scope.UserImage);
reader.onload = function(e){
var arrayBuffer = reader.result;
$scope.ImageArr = new Uint8Array(arrayBuffer);
};


I'm getting this
$scope.ImageArr
as
dict
in python having key-value list, then I get values from it like this

userByte =bytearray(ImageArr.values())


But when I save this
userByte
in mysql's Blob field, it saves some bytes there but that is not a proper image.

Any guesses what I'm doing wrong or what I'm missing?? Any kind of help will be appreciated.

Edited: (Updated code that worked for me)

First I convert my image to base64 through this code

reader = new FileReader();
reader.onloadend = function(e) { //callback after files finish loading
var img = e.target.result;
$scope.ImageArr = img;
}
reader.readAsDataURL($scope.UserImage); //once defined all callbacks, begin reading the file


then In python i encoded it in bytes like this

bytes(UserImage,"utf-8")


This works perfectly fine for me.

Answer

for as a hint you can save base64 image easily rather than byte arrays

Comments