I'm using webcam.js to capture user's image during user's registration on my template.
It prompts user permission and, if agreed, enable a "Take picture" button on page. On click, it takes a snapshot on the user's webcam. All working until saving the picture to django's ImageField.
Although it recommends using AJAX (which I can't understand in a million years) to upload it to my server, I've modified a little to pass the base64 string through an hidden input field. I've also managed to decode the base64 string and save it as a file '.jpeg' on the server successfully. I wish to save it from the memory, without writing it to the disk.
On models.py, foto
is the ImageField. I'm using Django 1.9.4 and Postgres 9.4
Enough talk, here's my coding attempt which saves everything but the image:
views.py
class view1(View):
def post(self, request, *args, **kwargs):
form = UserForm(request.POST or None)
form.foto = b64decode(request.POST['foto'][-(len(request.POST['foto']) - 24):])
#decoding 'foto' received from POST, some stripping is needed
if form.is_valid():
instance = form.save(commit=False)
instance.save()
return HttpResponseRedirect()
else:
return HttpResponseRedirect()
template
<form>
<input type='submit' value="Submit">
<input type='hidden' id='foto' name='foto'>
</form>
<!--- JS from webcam.js takes picture and pass b64 string to #foto--->
<!--- document.getElementById('foto').value="'"+data_uri+"'"; added to function "take_snapshot" --->
Any suggestions on how to manage the b64 string on my view?