python django框架 如何返回response头为Content-type:image/jpeg的图片文件

查看:903
本文介绍了python django框架 如何返回response头为Content-type:image/jpeg的图片文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

用php实现很简单

$fileres = file_get_contents('d:/a.jpg');
header('Content-type: image/jpeg');
echo $fileres;

关键词搜索无果,求助~

解决方案

from django.http import HttpResponse
f = open('d:/a.jpg', 'rb')
response = HttpResponse(f.read(), content_type='image/jpeg')

针对大的文件需要chunk的话可以使用FileReponse:

from django.http import FileResponse
response = FileResponse(open('d:/a.jpg', 'rb'), content_type='image/jpeg')

这篇关于python django框架 如何返回response头为Content-type:image/jpeg的图片文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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