# Send ImageField Attribute Images in Model by JsonResponse

**URL:** <https://forum.djangoproject.com/t/send-imagefield-attribute-images-in-model-by-jsonresponse/6606>\
**Category:** Using Django\
**Created:** [February 15, 2021, 10:02pm UTC](https://forum.djangoproject.com/t/send-imagefield-attribute-images-in-model-by-jsonresponse/6606 "2021-02-15T22:02:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![julianov](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/julianov/32/3502_2.png) [@julianov](https://forum.djangoproject.com/u/julianov)\
**Post date:** [February 15, 2021, 10:02pm UTC](https://forum.djangoproject.com/t/send-imagefield-attribute-images-in-model-by-jsonresponse/6606/1 "2021-02-15T22:02:57Z")

</div>

I send images through JsonResponse to clients but the problem is the encoding of the images  
the client cannot display them because of the encoding in ![]()

This is how I sends the images:

```
            data = [
            {'certificate': str(base64.b64encode(rubro.first().certificate.read())),
            'picture1': str(base64.b64encode(rubro.first().picture1.read())),
            'picture2': str(base64.b64encode(rubro.first().picture1.read())),
            'picture3': str(base64.b64encode(rubro.first().picture1.read()))},]

            return JsonResponse(data, safe=False)

```

If I try to directly send the ImageField like this:

```
            data = [
            {'certificate': rubro.first().certificate,
            'picture1': rubro.first().picture1,
            'picture2': rubro.first().picture1,
            'picture3': rubro.first().picture1},]

            return JsonResponse(data, safe=False)

```

I get: **TypeError: Object of type ImageFieldFile is not JSON serializable**

What I must do? How Can I send imagen from ImageField to clients?

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [February 15, 2021, 10:54pm UTC](https://forum.djangoproject.com/t/send-imagefield-attribute-images-in-model-by-jsonresponse/6606/2 "2021-02-15T22:54:30Z")

</div>

~~Can you explain why the first case in which you read the image into a base64 encoded string is unacceptable? I think I’m missing some context for your question and understanding that may help.~~

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 15, 2021, 11:49pm UTC](https://forum.djangoproject.com/t/send-imagefield-attribute-images-in-model-by-jsonresponse/6606/3 "2021-02-15T23:49:38Z")

</div>

The client is responsible for decoding the image and displaying it as a picture in the browser.

Also, please don’t keep adding new topics for this same issue - it’s a lot better to continue the previous thread, providing context for those reading it without you having to duplicate information posted in other threads.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 15, 2021, 11:53pm UTC](https://forum.djangoproject.com/t/send-imagefield-attribute-images-in-model-by-jsonresponse/6606/4 "2021-02-15T23:53:12Z")

</div>

This is now the third thread on this topic - if you’re interested in following along, start with reviewing the previous two threads.

---

<div class="post-metadata">

**Author:** ![julianov](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/julianov/32/3502_2.png) [@julianov](https://forum.djangoproject.com/u/julianov)\
**Post date:** [February 16, 2021, 1:32pm UTC](https://forum.djangoproject.com/t/send-imagefield-attribute-images-in-model-by-jsonresponse/6606/5 "2021-02-16T13:32:46Z")

</div>

The problem of this topic is different

Because if I send the following, it gives me a wrong encoding of the image

`str(base64.b64encode(rubro.first().certificate.read()))`

I can’t send the following

`base64.b64encode(rubro.first().certificate.read())`

because I get:

TypeError: Object of type bytes is not JSON serializable

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 16, 2021, 1:46pm UTC](https://forum.djangoproject.com/t/send-imagefield-attribute-images-in-model-by-jsonresponse/6606/6 "2021-02-16T13:46:14Z")

</div>

It’s still the same thread - you’re trying to send images from the server to the client - they’re all related issues.

But I don’t know what you mean by it giving you the “wrong encoding” of the image. That should give you the base64-encoded version of your image.

The JavaScript in the client is responsible for decoding that base64-encoded data back to the binary image to be added to the page.

---

<div class="post-metadata">

**Author:** ![julianov](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/julianov/32/3502_2.png) [@julianov](https://forum.djangoproject.com/u/julianov)\
**Post date:** [February 16, 2021, 1:48pm UTC](https://forum.djangoproject.com/t/send-imagefield-attribute-images-in-model-by-jsonresponse/6606/7 "2021-02-16T13:48:47Z")

</div>

because I must convert the base64-encode image to str to be able to sends it

If I set what server sends to this decode I get an error: [Best Online Base64 to Image Decoder / Converter](https://codebeautify.org/base64-to-image-converter)

So the base64-encode is not good

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 16, 2021, 2:08pm UTC](https://forum.djangoproject.com/t/send-imagefield-attribute-images-in-model-by-jsonresponse/6606/8 "2021-02-16T14:08:06Z")

</div>

See the docs for the [`bytes.decode`](https://docs.python.org/3/library/stdtypes.html#bytes.decode) method for converting a bytes object to a Python string.

You may also find it useful to review the Python docs on [Unicode](https://docs.python.org/3/howto/unicode.html), [Bytes](https://docs.python.org/3/library/stdtypes.html#bytes-objects), and [Strings](https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str) to get a better understanding of the relationships between Python strings and byte objects.
