[英]Why is flask corrupting base64 string from POST request?
I am using Ajax to POST a base64 string to a flask backend. 我正在使用Ajax将base64字符串发布到Flask后端。 I am creating the base64 string using the canvas.toDataURL('image/png') to create a snapshot of the canvas.
我正在使用canvas.toDataURL('image / png')创建base64字符串来创建画布的快照。 You can see the ajax request below.
您可以在下面看到ajax请求。
img.src = canvas.toDataURL('image/png');
$.ajax({
url: "http://0.0.0.0:9000/postimage",
type: "POST",
crossDomain: true,
processData: false,
data: encodeURI(img.src),
success: function (response) {
console.log(response);
},
error: function (xhr, status) {
console.log(xhr)
console.log(status)
console.log("error");
}
});
However, when the base64 is received on the backend it is corrupted. 但是,当在后端接收到base64时,它已损坏。 It does not have the trailing '==' and there is lots of spaces where previously there was '+' or '++'.
它没有尾随的“ ==”,并且在以前有“ +”或“ ++”的地方有很多空格。 When I try and access the string via request.data I get an empty binary string b''.
当我尝试通过request.data访问字符串时,我得到一个空的二进制字符串b''。 I have been trying to access the base64 string via request.form but oddly that returns the base64 string in a dictionary as the KEY not the value, which is why I have image = key.
我一直在尝试通过request.form访问base64字符串,但是奇怪的是,它在字典中返回base64字符串作为KEY而不是值,这就是为什么我有image = key的原因。
@app.route('/postimage', methods=['POST'])
def post_image():
if request.method == 'POST':
d = request.form
print(d)
for key, value in d.items():
image = key
image += '=='
image = image.split(',', 1)[-1]
imageEncoded = base64.decodestring(image.encode())
with open("sean-sean.png", "wb") as fh:
fh.write(imageEncoded)
Suggestions? 有什么建议吗?
使用encodeURIComponent()代替不编码保留字符(例如'=','+')的encodeURI()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.