IPFS in browser: testing a simple file fetch

614 Views Asked by At

I want to download this file and print its contents to the console using an in-browser IPFS node. The following html file should do the job:

<!doctype html>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>
</title>
  </head>
  <body>
      <script type='text/javascript' src='https://unpkg.com/[email protected]/dist/index.min.js'></script>
      <script type="text/javascript">

var ifile = 'Qmc3zqKcwzbbvw3MQm3hXdg8BQoFjGdZiGdAfXAyAGGdLi';
(async () => {
    const inode = await Ipfs.create({
        config: {
            Addresses: {
                Swarm: [
                    // These webrtc-star servers are for testing only
                    '/dns4/wrtc-star1.par.dwebops.pub/tcp/443/wss/p2p-webrtc-star',
                    '/dns4/wrtc-star2.sjc.dwebops.pub/tcp/443/wss/p2p-webrtc-star'
                    ]
                },
                Bootstrap: []
        }
    })
    window.inode = inode; //For poking
    
    for await (const chunk of inode.cat(ifile)) {
         console.log(chunk.toString());
    }
})();

      </script>
      Testing IPFS file fetch
</body></html>

But it doesn't print anything. What am I missing?

1

There are 1 best solutions below

0
On BEST ANSWER

You don't have any bootstrap nodes, so it can't find the CID. If you add my node for example, it works fine:

<!doctype html>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>
</title>
  </head>
  <body>
      <script type='text/javascript' src='https://unpkg.com/[email protected]/dist/index.min.js'></script>
      <script type="text/javascript">

var ifile = 'Qmc3zqKcwzbbvw3MQm3hXdg8BQoFjGdZiGdAfXAyAGGdLi';
(async () => {
    const inode = await Ipfs.create({
        config: {
            Addresses: {
                Swarm: [
                    // These webrtc-star servers are for testing only
                    '/dns4/wrtc-star1.par.dwebops.pub/tcp/443/wss/p2p-webrtc-star',
                    '/dns4/wrtc-star2.sjc.dwebops.pub/tcp/443/wss/p2p-webrtc-star'
                    ]
                },
                Bootstrap: []
        }
    })
    window.inode = inode; //For poking
    
    await inode.swarm.connect("/dns6/ipfs.thedisco.zone/tcp/4430/wss/p2p/12D3KooWChhhfGdB9GJy1GbhghAAKCUR99oCymMEVS4eUcEy67nt");
    
    for await (const chunk of inode.cat(ifile)) {
         console.log(chunk.toString());
    }
})();

      </script>
      Testing IPFS file fetch
</body></html>