Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

217
Visualizações
Sending image from front end(JavaScript) to backend(Django)

I have a image at the front end and I want to send it to the backend. I have simple JavaScript at the front end and Django-python at the backend. I have tried dozens of different methods and spent weeks but I am not able to figure this out. Can you guys suggest how can I achieve this on front end(sending part) and backend(receiving part). A piece of code with an advice would be really appreciated. I am using function based views on the backend. Can someone help me out with the backend code (if the front end code is right) to handle incoming from the front end. Because I have first converted the image file to base 64 stream in order to send it to the backend through fetch api.

Front end code:


   const { snippet, transform } = anno.getImageSnippetById(annotation.id) //this function returns a DOM canvas element in snippet variable
       const base64Canvas = snippet.toDataURL("image/jpeg").split(';base64,')[1]; //converting into a base64 stream
       fetch(url1,{
                 method:'POST',
                 headers:{
                   'Content-type':'application/json',
                   'X-CSRFToken':csrftoken,
                 },
                 body: {'snippet':base64Canvas}
               })

Backend code:

def snippetCreate(request):
   serializer = Annotated_SnippetsSerializer(data=request['snippet'])
   image = base64_to_image(base64_string=serializer.data)
   img = Annotated_Snippets(asnippet=image)
   img.save()
   return Response({'success':'success'}) ```

base64_to_image:
```def base64_to_image(base64_string):
   format, imgstr = base64_string.split(';base64,') 
   ext = format.split('/')[-1]
   return ContentFile(base64.b64decode(imgstr), name=uuid.uuid4().hex + "." + ext) ```
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda