Django休息框架:如何直接在体内提供二进制文件

时间:2017-03-20 10:43:41

标签: django image rest download django-rest-framework

我需要在body请求中直接返回一个Image(二进制),但我得到的是一个没有扩展名且内部为空数组/ json的文件!

我正在使用python 3,Django == 1.10.5和djangorestframework == 3.5.3,drf-extensions == 0.3.1(对于嵌套路由)和django-extra-fields == 0.9 for ImageField

(我试过没有django-extra-fields,它是一样的)

我已经在这里找到了一个解决方案(很多Enix; p)和base64:Django rest framework : How to download image with this image send directly in the body

但我的老板不想要base64,只想在身体反应中使用二进制文件。

我的models.py

class Image(models.Model):
    class Meta:
        verbose_name = _('Image')
        verbose_name_plural = _('Images')

    creation_date = models.DateTimeField(
        help_text=_('Creation date'),
        auto_now_add=True,
        editable=False
    )

    modified_date = models.DateTimeField(
        help_text=_('Last modification date'),
        auto_now=True
    )

    image_file = models.ImageField(upload_to='', null=True)

我的serializers.py

class ImageSerializer(serializers.ModelSerializer):

    class Meta:
        model = Image
        fields = ('image_file',)

我的views.py

class ImageViewSet(NestedViewSetMixin, viewsets.ModelViewSet):

    http_method_names = ['get', 'put']
    queryset = Image.objects.all()
    serializer_class = ImageSerializer
    pagination_class = None

    def get_queryset(self, *args, **kwargs):

        image = Image.objects.last()  # This is just for the exemple.
        filename = image.image_file
        size = filename.size
        response = FileResponse(open(filename.path, 'rb'), content_type="image/png")
        response['Content-Length'] = size
        response['Content-Disposition'] = "attachment; filename=%s" % 'notification-icon.png'

        return response 

我做了一些测试:

  

django.core.files.File,filewrapper,停用序列化程序

但没有产生预期的结果...

如果有人发现我做错了或者在设置中遗忘了什么?任何有关相同的帮助将非常感谢!

1 个答案:

答案 0 :(得分:0)

好的,我发现了我的错误......

我试图覆盖get_querryset ...而不是因为我需要覆盖列表方法而得不到的get方法!

我需要多睡一点; p

这是一个对我来说很有用的解决方案,如果需要,可以使用Streaming下载大文件:

我的models.py

class Image(models.Model):
    class Meta:
        verbose_name = _('Image')
        verbose_name_plural = _('Images')

    creation_date = models.DateTimeField(
        help_text=_('Creation date'),
        auto_now_add=True,
        editable=False
    )

    modified_date = models.DateTimeField(
        help_text=_('Last modification date'),
        auto_now=True
    )

    image_file = models.ImageField(upload_to='', null=True)

我的serializers.py

class ImageSerializer(serializers.ModelSerializer):

    class Meta:
        model = Image
        fields = ('image_file',)

我的views.py

class ImageViewSet(NestedViewSetMixin, viewsets.ModelViewSet):

    http_method_names = ['get', 'put']
    queryset = Image.objects.all()
    serializer_class = ImageSerializer
    pagination_class = None

        def list(self, request, *args, **kwargs):

            image = Image.objects.get(pk=get_your_good_record)
            filename = image.image_file
            size = filename.size

            content_type_file = mimetypes.guess_type(filename.path)[0]

            response = StreamingHttpResponse(open(image.image_file.path, 'rb'), content_type=content_type_file)
            response['Content-Disposition'] = "attachment; filename=%s" % str(filename)
            response['Content-Length'] = size

            return response

如此简单; p