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)

Captura de pantalla 2018-07-15 a las 14.50.54

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

Captura de pantalla 2018-07-15 a las 14.35.31

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

Advertisements
Posted by:.

2 replies on “How to push/load image file from/to IPFS using Javascript. Examples (PART II)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s