简体   繁体   English

Django / Python / toDataURL返回带有toDataURL字符串的响应

[英]Django/Python/toDataURL return a response with the string from toDataURL

I have saved in a database a string return by the javascript method toDataURL. 我已将通过javascript方法toDataURL返回的字符串保存在数据库中。 An exemple is available here: http://pastebin.com/0Qu8rngD 此处提供示例: http : //pastebin.com/0Qu8rngD

I need to return the image in a django response. 我需要在django响应中返回图像。 Something like: 就像是:

return HttpResponse(image, mimetype='image/png')

I have tried many methods with base64decode, urlsafe_b64decode, Image ... with no success. 我尝试使用base64decode,urlsafe_b64decode,Image ...的许多方法都没有成功。 The navigator does not display the image, it can't read the response data. 导航器不显示图像,无法读取响应数据。

Of course I can display my image in a HTML page with <img src="{{image}}"> , it works well. 当然,我可以使用<img src="{{image}}">在HTML页面中显示图像,效果很好。

The image is base64 encoded in the data uri; 图像是在数据uri中以base64编码的; decode the image first: 首先解码图像:

import base64

...    

data_uri = 'data:...'
image_data = data_uri.partition('base64,')[2]
binary = base64.b64decode(image_data)
return HttpResponse(binary, mimetype='image/png')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM