【发布时间】: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