I need to return an Image directly in the body request, but i get in response a generated file with no extension and an empty array/json inside...
I'm using python 3, Django==1.10.5 and djangorestframework==3.5.3
My 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)
My serializers.py
class ImageSerializer(serializers.ModelSerializer):
class Meta:
model = Image
fields = ('image_file',)
My 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()
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
If someone find what I'm doing wrong, I will really appreciate. Thx a lot ;p
Edit : i have tryed with django.core.files.File, filewrapper and tryed to desactivate the serializer without effect.
You can use
base64
module to encode the image file to base64 format (a raw string), and then assigned it to a field to transfer your image.I have updated your
ImageSerializer
to include a new filed namedbase64_image
which is encoded from the model's image file with base64 format.The following example is for your reference:
serialisers.py
views.py
urls.py
Finally, you can open a browser with url:
http://localhost:8000/image/
, and you will get the response like that:How to show your image in front-end or app?
When your front-end get the json above, you need to convert the base64 format back to image raw data.
For Javascript:
How can I set Image source with base64
For iOS:
how to decode BASE64 encoded PNG using Objective C
For Android:
How to convert a Base64 string into a BitMap image to show it in a ImageView?
I have upload an example to GitHub. Hope it would help.