为什么烧瓶会破坏POST请求中的base64字符串?

时间:2018-03-19 07:51:30

标签: python ajax flask base64

我正在使用Ajax将base64字符串发布到烧瓶后端。我正在使用canvas.toDataURL(' image / png')创建base64字符串,以创建画布的快照。您可以在下面看到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");
            }
        });

但是,当在后端接收到base64时,它已损坏。它没有尾随' =='并且有很多空间以前有过' +'或者' ++'。当我尝试通过request.data访问字符串时,我得到一个空的二进制字符串b''。我一直试图通过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)

连连呢?

1 个答案:

答案 0 :(得分:0)

使用encodeURIComponent()而不是encodeURI(),它不编码保留字符,例如'=','+'