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

372
Visualizações
Saving js dataUrl to file using qml and c++

I'm trying to get a dataUrl object from javascript and convert it to a file by sending it from the website to qml and then to a file.

I have the following code to do this but it doesn't seem to work as intended as the saved file (for example an image) is blank.

In javascript I'm taking the dataUrl and get the base64 part from it.

  async function handleDownload(e) {
    if (link !== null || url != null) {
      e.preventDefault();
      const base64 = url != null ? url.split(',')[1] : link.split(',')[1];
      QML.handleDownload(base64, name);
    }
  }

In qml I have just a simple function that transfers the base64 string to the c++ backend:


function handleDownload(fileBase64, fileName) {
    console.log("Downloaded " + Backend.saveBase64(fileBase64, fileName))
}

And in C++ I'm using the following function to save it to a file'

QString Backend::saveBase64(QString fileBase64, QString fileName) {
    const QByteArray base64Array = fileBase64.toUtf8().toBase64();
    QString path("/home/user/Downloads/" + QString(QDir::separator()) + fileName);
    QFile file(path);
    file.open(QIODevice::WriteOnly);
    file.write(base64Array);
    file.close();
    return path;
}

I want to also add that the dataUrl isn't just an image file but can also be any other file type.

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

0

The question can be also used as an minimal example given that a dataUrl looks something like this: data:[<mediatype>][;base64],<data> and also giving qml and the feature allowing you to communicate between javascript qml and qml cpp.

Anyway. In the end I managed to come up with the following function.


QString Backend::saveBase64(QString fileBase64, QString fileName) {
    QString path("/home/phablet/.cache/cinny.nitanmarcel" + QString(QDir::separator()) + fileName);
    QFile file(path);
    file.open(QIODevice::WriteOnly);
    file.write(QByteArray::fromBase64(fileBase64.toLocal8Bit()));
    file.close();
    return path;
}

What I was doing wrong is that I took the base64 string and converted it again to base64 which resulted in the data being corrupted.

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