3

I know how to upload file in IPFS but what if I want to upload simple JS array of strings? it returns unknown file and I can't see it's content

var bufferFile = Buffer.Buffer.from(uuidArray);
ipfs.files.add(bufferFile,(error,result)=>{
  console.log(result[0].hash);
})
O. Shekriladze
  • 1,346
  • 1
  • 19
  • 36

2 Answers2

0

Sure, you use addAll to add an array of strings, here's a full example:

<!DOCTYPE html>
<html lang="en">
<head><title>js-ipfs minimal add example</title>

<script src="https://cdn.jsdelivr.net/npm/ipfs-core@0.15.2/dist/index.min.js"></script>

<script>
var ipfs;
async function main() {
    // Create our IPFS node
    ipfs = await window.IpfsCore.create();

    // Build our "files" array
    const files = [{
        content: 'ABC'
    },{
        content: 'DEF'
    }];

    // Use ipfs.addAll to add each string as it's own CID, then output the results
    for await (const result of ipfs.addAll(files)) {
        console.log(result);
    }
}
main()
</script>
</head>
</html>

Output:

Object { path: "QmNz1UBzpdd4HfZ3qir3aPiRdX5a93XwTuDNyXRc6PKhWW", cid: {…}, size: 11, mode: 420, mtime: undefined }
Object { path: "QmPF99trksANxfkCExBZt9veeitXEcYLtCiacEoh71Eyx4", cid: {…}, size: 11, mode: 420, mtime: undefined }
Discordian
  • 749
  • 3
  • 16
-1

I don't think you can upload strings to the IPFS but what you can do is write it to a file and upload the file with your array of strings.

LordDraagon
  • 521
  • 12
  • 31