图片未正确保存在 blob 字段中(Angular 应用到 Python)

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

我正在尝试将图像从 angular App 保存到 python 中的 mysql blob 字段,但图像未正确保存。

这就是我将图像转换为字节数组的方式

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

我在 python 中得到这个 $scope.ImageArr 作为 dict 具有键值列表,然后我从中得到值像这样

userByte =bytearray(ImageArr.values())

但是当我将此 userByte 保存在 mysql 的 Blob 字段中时,它会在那里保存一些字节,但这不是正确的图像。

有没有人猜到我做错了什么或遗漏了什么??任何形式的帮助将不胜感激。

已编辑:(对我有用的更新代码)

首先我通过这段代码将我的图像转换为base64

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

然后在python中我用这样的字节编码它

bytes(UserImage,"utf-8")

这对我来说非常好。

作为 hint 您可以轻松保存 base64 图像而不是字节数组