Django返回jpg图像数据

 借鉴:https://blog.csdn.net/baidu_39416074/article/details/80926818

# 1.03 读取图片demo
def read_img(request):
    """
    : 读取图片
    :param request:
    :return:
    """
    try:
        data = request.GET
        file_name = data.get("file_name")
        imagepath = os.path.join(settings.BASE_DIR, "static/resume/images/{}".format(file_name))  # 图片路径
        with open(imagepath, 'rb') as f:
            image_data = f.read()
        return HttpResponse(image_data, content_type="image/png")                //这句是关键
    except Exception as e:
        print(e)
        return HttpResponse(str(e))

 return HttpResponse(image_data, content_type="image/png") 

image_data:图片的二进制格式

content_type="image/png":固定不变的写法

发布了58 篇原创文章 · 获赞 3 · 访问量 5713

猜你喜欢

转载自blog.csdn.net/hc1428090104/article/details/99356519