Last active
January 29, 2018 15:46
-
-
Save karmadonov/3305365 to your computer and use it in GitHub Desktop.
Upload base64 file from POST request to Django form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
import cStringIO | |
from django.core.files.uploadedfile import InMemoryUploadedFile | |
# ... | |
if request.POST.get('file') and request.POST.get('name'): | |
file = cStringIO.StringIO(base64.b64decode(request.POST['file'])) | |
image = InMemoryUploadedFile(file, | |
field_name='file', | |
name=request.POST['name'], | |
content_type="image/jpeg", | |
size=len(file.getvalue()), | |
charset=None) | |
request.FILES[u'file'] = image | |
# ... |
Your size calculation produces truncated files size=sys.getsizeof(file)
I'm guessing it's because of buffered IO.. dunno.
This works for me: size=len(file.getvalue())
:)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You forgot to import sys :)