0

I use ipfs-http-client to upload and retrieve my file. The following creates the client and uploads an image in base64 string:

import { create } from "ipfs-http-client";
let client = create("/ip4/127.0.0.1/tcp/5001");
let file = await client.add(somebase64imagestring);

Below is how I retrieve the file given the CID

let iter = client.cat(imageCID);
for await (let img of iterable) {
  const imgStr = Buffer.from(img).toString();
}

The problem is intermittent. Sometimes, I get the full base64 string in imgStr which is 19.9kb. Sometimes, I only get a portion of it like 11.8kb where the first few characters are missing. Even if I await on the client.cat which the VSCode highlights as no effect, and even if I do client.cat(imageCID, {offset: 0}), still the returned image string is sometimes complete, and sometimes missing the first few parts.

iPhoneJavaDev
  • 821
  • 5
  • 33
  • 78

1 Answers1

0

Looks like the iter part may return the image in chunks, so I concat the incoming chunks instead:

let imgStr = "";
let iter = client.cat(imageCID);
for await (let img of iterable) {
  imgStr += Buffer.from(img).toString();
}

Now, I am able to get the complete image string. :)

iPhoneJavaDev
  • 821
  • 5
  • 33
  • 78