Save base64 image in django file field
<p>I have following input</p>

<pre><code>"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA7YAAAISCAIAAAB3YsSDAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAA5JxJREFUeNrsnQl4FEX6xqcJJEAS7ivhBkMAQTSJ4h0QEQ+I90rAc1cOL3QBXXV1AV1dVwmrsCqQ9VwJ6HoC7oon0T8iEkABwRC5IeE+kkAIkPT/nfmSmprunskk5CDw/p55hu7qOr76api8........"
</code></pre>

<p>I want to save this file in file field. What can I do?</p>

<p><strong>models.py</strong></p>

<pre><code>class SomeModel(models.Model):
    file = models.FileField(upload_to=get_upload_report)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
</code></pre>

<p>I'm trying to do this</p>

<pre><code>def get_file(data):
    from django.core.files import File
    return File(data)
</code></pre>

<p>and save return file to model instance</p>

<pre><code>somemodel.file = get_file(image_base64_data)
</code></pre>

<p>but it's gives a following error</p>

<pre><code>AttributeError at /someurl/

'File' object has no attribute 'decode'
</code></pre>

<p>This question looks like it should help: <a href="http://stackoverflow.com/questions/7514964/django-how-to-create-a-file-and-save-it-to-a-models-filefield">Django - how to create a file and save it to a model&#39;s FileField?</a> </p>

<p>You should be able to decode the base64 string and supply that as the <code>content</code> argument to <a href="https://docs.djangoproject.com/en/1.9/ref/models/fields/#django.db.models.fields.files.FieldFile.save" rel="nofollow"><code>FieldFile.save</code></a>: </p>

python django image filefield