【发布时间】:2019-03-31 03:10:06
【问题描述】:
我需要使用 blob 将图像从客户端发送到服务器
我已经在 jquery(客户端)中将图像转换为 BLOB 并将 blob 发送到 python 烧瓶(服务器端)问题是,我无法从 BLOB 重新创建图像。我在python中尝试了以下代码,但无法获取图像
将图像转换为 blob 的 Jquery 代码:
function changeFile() {
var file = this.files[0];
var reader = new FileReader();
reader.addEventListener('load', readFile);
reader.readAsText(file);
}
function readFile(event) {
document.body.textContent = event.target.result;
console.log(event.target.result);
appendFileAndSubmit(event.target.result);
}
function appendFileAndSubmit(imageUrl){
var ImageURL = imageUrl
var data = {
'logo':ImageURL
}
$.ajax({
url: 'http://sdsdss/User/imgBlob',
method: 'POST',
dataType : 'json',
data: JSON.stringify(data),
contentType: 'application/json; charset=utf-8'
}).done(function(response){
var datas = JSON.parse(response);
console.log(datas);
});
}
document.getElementById("inp").addEventListener("change", changeFile);
Python 代码:将BLOB 重新创建为图像
function getImage(self):
reqData = json.loads(request.data)
Logo = reqData['logo']
png_recovered = base64.decodestring(Logo)
f = open("temp.png", "w")
f.write(png_recovered)
f.close()
【问题讨论】:
标签: javascript python ajax flask blob