Skip to content

how to decode base64 data into image django – js

im trying to save captured image ( encoding data) into database from canvas , but it only saves a blank image ?

here is my code

    const player = document.getElementById('player');
    const docs = document.getElementById('document')

    const captureButton = document.getElementById('capture');
    const constraints = {
      video: true,
    captureButton.addEventListener('click', (e) => {
      const canvas = document.getElementById('canvas');
      const context = canvas.getContext('2d');
      const imgFormat = canvas.toDataURL();
      docs.value = imgFormat 
      context.drawImage(player, 0, 0, canvas.width, canvas.height);
      .then((stream) => {
        player.srcObject = stream;
            <form action="" method="POST" enctype="multipart/form-data" dir="ltr">{% csrf_token %}

                <input type="text" name="documents" id="document">
                <video id="player" controls autoplay></video>
                <button id="capture">Capture</button>
                <canvas id="canvas" width=320 height=240></canvas>

                <button class="header pt-2  text-white px-4 p-1 rounded-lg mt-4">{% trans "save" %}</button>

and here is my with

class Document(models.Model):
    booking =models.ForeignKey(Booking,on_delete=models.PROTECT)
    docs = models.ImageField(upload_to=upload_docs)


import base64
from django.core.files.base import ContentFile
def add_new_image(request,id):
    obj = get_object_or_404(Booking,id=id)
    if request.method == 'POST':
        data = request.POST.get('documents')
        format, imgstr = data.split(';base64,') 
        ext = format.split('/')[-1] 

        data = ContentFile(base64.b64decode(imgstr), name='temp.' + ext)      
        if data:      
            photo = Document.objects.create(
                booking = obj,
                docs = data

        return redirect(reverse_lazy("booking:add_booking",kwargs={"room_no":obj.room_no.room_no}))
        messages.error(request,_('choose or capture right image ..'))
    return render(request,'booking/add_img.html',{'obj':obj,'form':images})

i much appreciate your helps , please if you know something about it let me know , thank you so much


You need to draw first then get the content.

For how to do multiple capture.

You can reuse the same canvas, the toDataURL is a snapshot of current canvas.

Here is an example.

const player = document.getElementById('player')
const canvas = document.getElementById('canvas')
const form = document.getElementById('form')
const docs = document.getElementById('document')
const captureButton = document.getElementById('capture')
const context = canvas.getContext('2d')

captureButton.addEventListener('click', (e) => {
  context.drawImage(player, 0, 0, canvas.width, canvas.height)
  // this is just an example, you need a <input> so it's send by form (or use async request)
  let new_image = document.createElement('img')
  new_image.src = canvas.toDataURL()

navigator.mediaDevices.getUserMedia({video: true})
  .then((stream) => { player.srcObject = stream;})
form *{max-width:20vw;}
<form id="form" action="" method="POST" enctype="multipart/form-data">
  <video id="player" controls autoplay></video>
  <button type="button" id="capture">Capture</button>
  <canvas id="canvas" width=320 height=240></canvas>