Previous post: How to push/load image file from/to IPFS using Javascript. Examples (PART I)
In this post, we want to show how IPFS works using only HTML and Javascript (This is not another Node.js server example code…)
In this example we only push 1 text file and we read it. ( see it working)
The code is similar to the previous post.
Continuing, if you inspect the code, you will see we load always the script resource:
https://unpkg.com/ipfs/dist/index.js
And then we wait the loading of IPFS to start the push of file and the read of it after that.
const repoPath = 'ipfs-' + Math.random() const ipfs = new Ipfs({ repo: repoPath }) ipfs.on('ready', () => { const files = [ { path: 'MrAddon.txt', content: ipfs.types.Buffer.from('MrAddon Rocks', 'utf8') } ] ipfs.files.add(files, function (err, files) { //ipfsPath = files[0].hash; log("Storing file MrAddon.txt on IPFS using Javascript. HASH:" + files[0].hash); ipfsPath = files[0].hash ipfs.files.cat(ipfsPath, function (err, file) { if (err) { throw err } log("Content of HASH retrieved from IPFS: " + file.toString('utf8')); }) }) const log = (line) => { document.getElementById('output').appendChild(document.createTextNode(`${line}\r\n`)) } })
By MrAddon
Here a more actually example of IPFS communication using HTML and Javascript
https://mraddon.blog/2019/04/29/html-and-javascript-code-to-load-a-file-in-your-ipfs-server/
LikeLike