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

116
Visualizações
How to save pdf to Cloudant

I want to save the pdf to Cloudant. With the code below, I get an error opening the Attachment in Cloudant. "An error was encountered when processing this file" I can put fake string data in the "._attachments[name].data" field and it will save.

The Cloudant docs say the data content needs to be in base64 and that is what I am attempting. Cloudant says "The content must be provided by using BASE64 representation"

 function saveFile() {
      var doc = {};
      var blob = null;
      //fileName is from the input field model data
      var url = fileName;
    
  fetch(url)
    .then((r) => r.blob())
    .then((b) => {
      blob = b;
      return getBase64(blob);
    })
    .then((blob) => {
      console.log(blob);
      let name = url._rawValue.name;

      doc._id = "testing::" + new Date().getTime();
      doc.type = "testing attachment";

      doc._attachments = {};
      doc._attachments[name] = {};
      doc._attachments[name].content_type = "application/pdf";
      doc._attachments[name].data = blob.split(",")[1];
      console.log("doc: ", doc);
    })
    .then(() => {
      api({
        method: "POST",
        url: "/webdata",
        auth: {
          username: process.env.CLOUDANT_USERNAME,
          password: process.env.CLOUDANT_PASSWORD,
        },
        data: doc,
      })
        .then((response) => {
          console.log("result: ", response);

          alert("Test has been submitted!");
        })
        .catch((e) => {
          console.log("e: ", e);
          alert(e);
        });
      console.log("finished send test");
    });
}
function getBase64(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = (error) => reject(error);
  });
}

any ideas? Thanks

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

0

CouchDB, and by extension Cloudant, has a means of handling a "multi-part" request where the JSON document and the attachments are sent in the same request. See https://docs.couchdb.org/en/3.2.2/api/document/common.html#put--db-docid

They are modelled in CouchDB's Nano project here: https://www.npmjs.com/package/nano#multipart-functions

const fs = require('fs');

fs.readFile('rabbit.png', (err, data) => {
  if (!err) {
    await alice.multipart.insert({ foo: 'bar' }, [{name: 'rabbit.png', data: data, content_type: 'image/png'}], 'mydoc')
  }
});

Alternatively, you could write the document first and add the attachment in a supplementary request. Using the current Cloudant SDKs:

  • write document https://cloud.ibm.com/apidocs/cloudant?code=node#putdocument
  • write attachment https://cloud.ibm.com/apidocs/cloudant?code=node#putattachment
const doc = {
  a: 1,
  b: 2
}
const res = await service.putDocument({
  db: 'events',
  docId: 'mydocid',
  document: doc
})
const stream = fs.createReadStream('./mypdf.pdf')
await service.putAttachment({
  db: 'events',
  docId: 'mydocid',
  rev: res.result.rev,  // we need the _rev of the doc we've just created 
  attachmentName: 'mypdf',
  attachment: stream,
  contentType: 'application/pdf'
})
about 4 years ago · Juan Pablo Isaza Relatório

0

I found out I was doing too much to the PDF file. No need to make to blob then convert to base64.

Only convert to base64.

async function sendFiles() {
  try {
      const url = fileName;
      const doc = {};
      doc._attachments = {};
      doc._id = "testing::" + new Date().getTime();
      doc.type = "testing attachment";

    for (let item of url._value) {
     
      const blob2 = await getBase64(item);

      let name = item.name;
      doc._attachments[name] = {};
      doc._attachments[name].content_type = item.type;
      doc._attachments[name].data = blob2.split(",")[1];
     
    }
    const response = await api({
      method: "POST",
      url: "/webdata",
      data: doc,
    });
   
  } catch (e) {
    console.log(e);
    throw e; // throw error so caller can see the error
  }
  console.log("finished send test");
  fileName.value = null;
}
function getBase64(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = (error) => reject(error);
  });
}

This works for me.

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