Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

118
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda