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

290
Visualizações
Send canvas.toDataURL images to nodejs

I'm trying to send image from front-end script to my server.

Front-end script:

var img_data = canvas.toDataURL('image/jpg'); // contains screenshot image
// Insert here POST request to send image to server

And I'm trying to accept the data in the backend and store it into req.files to be able to access like this:

const get_image = (req, res) => {
    const File = req.files.File.tempFilePath;
}

What way can I do to send the image to the server and get the image like in the example above?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

your img_data is a base 64 string, which you can send to server directly in a post request

e.g.

await fetch('/api/path', { method: 'POST', headers: { "content-type": "application/json"}, body: JSON.stringify({ file: img_data }) });

On your backend, you can convert this string to binary, and save to file.

var fs = require('fs');

app.post('/api/path', async (req, res) => {
     const img = req.body.file;
     var regex = /^data:.+\/(.+);base64,(.*)$/;

     var matches = string.match(regex);
     var ext = matches[1];
     var data = matches[2];
     var buffer = Buffer.from(data, 'base64'); //file buffer
      
     .... //do whatever you want with the buffer

     fs.writeFileSync('imagename.' + ext, buffer); //if you do not need to save to file, you can skip this step.
     
     ....// return res to client


})
about 4 years ago · Juan Pablo Isaza Relatório

0

You have to convert it to a Blob first, and then append it to a Form. The form would be the body of the request that you send to server.

canvas.toBlob(function(blob){
    var form = new FormData(),
    request = new XMLHttpRequest();

    form.append("image", blob, "filename.png");
    request.open("POST", "/upload", true);
    request.send(form);
}, "image/png");
about 4 years ago · Juan Pablo Isaza Relatório
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