【问题标题】:Image not saving properly in blob field (Angular App to Python)图像未正确保存在 blob 字段中(Angular App to Python)
【发布时间】:2017-03-08 09:54:06
【问题描述】:

我正在尝试将图像从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")

这对我来说非常好。

【问题讨论】:

    标签: python mysql angularjs image blob


    【解决方案1】:

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

    【讨论】:

    • 你的提示完成了我的工作......我在我的问题编辑部分添加了最新代码。谢谢
    猜你喜欢
    • 2015-02-05
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    • 2011-07-23
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多