B
B
borisovdenis2017-04-06 14:08:00
JavaScript
borisovdenis, 2017-04-06 14:08:00

Save base64 string sent from browser to Django server?

The situation is as follows: I use the html2canvas plugin to convert a div block into a canvas, then I convert this canvas using the toDataURL method into a png image. I get a string like this data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA+gAAADPCAYAAABm4vv3AAAgAElEQVR4Xu29V3BcSXrn+xW89.......
Task: Convert Div to image, send to Django server and save this image.
The problem is that it can't be saved.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
Zlatoslav Desyatnikov, 2017-04-06
@borisovdenis

Here is the answer to your question:
stackoverflow.com/a/15376482

E
Elvis, 2017-04-06
@Dr_Elvis

Perhaps late, but I also did saving the image from Base64. Everything works like clockwork.

def b64_to_file(x):
    imgData = x.split(',', maxsplit=1)[1]# Берем всё что находится после запятой, то есть сам Base64
    ImgGUID = str(uuid.uuid4())#Генерируем гуид для названия картинки
    imgSaveURL = settings.STATIC_ROOT + 'img/' + ImgGUID + '.png'# Путь куда сохранять картинку
    with open(imgSaveURL, "wb") as fh:
        fh.write(base64.decodestring(imgData.encode()))# Сохраняем

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question