【发布时间】:2018-08-07 17:13:01
【问题描述】:
我的烧瓶 python 应用程序返回一个图像作为 POST 请求的响应:
def upload_file():
if request.method == 'POST':
return send_file('1.png', mimetype='image/png')
那我想用javascript和jquery ajax做请求,得到的响应是:
success: function(img_response) {
console.log(img_response);
}
�PNG ���� IHDR����������?1�� ��IDATx����nIv��vdU�Ѓ�ۀm6f`������?��W3�1��%6Y��ER�xh�Pb��]�R�DfeFF�qo��_ ����O�4�]J$��.�%E�%E�ɲ$)...
如何在浏览器中呈现这种类型的文件? 也许更好地解码烧瓶中base64中的图像,但我该怎么做呢?
【问题讨论】:
-
文件不作为二进制文件发送到浏览器。 HTTP 协议要求这个文件是字符编码的。这发生在幕后。为了在客户端上使用该文件,您可以从服务器创建带有该文件的新 Image() 对象并将其放入 DOM 中。不用担心二进制/编码/解码等
-
谢谢。那么我该怎么做呢?我使用它没有成功:
success: function(img_response) { $('#image').html('<img src="data:image/png;base64,' + img_response + '" />'); },
标签: javascript image flask base64