使用Django HttpResponse返回二进制数据 [英] Returning binary data with django HttpResponse

查看:875
本文介绍了使用Django HttpResponse返回二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图让Django的HttpResponse返回二进制数据,但没有成功.我已经尝试了一段时间的不同方法,但是没有成功.

I'm trying to get Django's HttpResponse to return binary data without much success. I've been trying different methods for a while now, but without success.

只要二进制数据值不在ASCII字符范围(小于0-255)的范围内,就可以将字符串编码为ASCII.使用latin-1进行编码时也发生了同样的情况.

Encoding the string to ASCII works as long as the binary data values aren't outside the ASCII char scope, which is smaller than 0-255. The same happened when encoding with latin-1.

创建字节字符串的效果很好,但是如果包含某些值,则创建失败,例如,如果我在数据中包含以下字节:"\ xf6 \ x52",结果将得到不同的字节.由于某种原因,当我尝试查看结果响应时,第一个字节\ xf6被转换为0xfffd.

Creating byte string works nicely, but seems to fail if certain values are included, for example, if I have the following bytes included in the data: "\xf6\x52", I will get different bytes as a result. For some reason, the first byte, \xf6, gets converted to 0xfffd when I'm trying to review the result response.

我希望获得一些反馈并对此提供帮助.

I'd love to get some feedback and help with this.

非常感谢!

-A-

推荐答案

return HttpResponse(data, content_type='application/octet-stream')

为我工作.

这篇关于使用Django HttpResponse返回二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆