Previous posts:

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 PNG image files and we read it. ( see it working)

Captura de pantalla 2018-07-15 a las 15.19.23

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

We use this code to use 2 functions, one to upload PNG files to IPFS, another one to read PNG files from the HASH (using IPFS).


 function loadFile() {
        var input, file, fr;

        input = document.getElementById('fileinput');

        file = input.files[0];
        fr = new FileReader();
        fr.onload = receivedText();
        

        function receivedText() {
            fr = new FileReader();
            fr.readAsBinaryString(file);
            
            //IPFS START
            const repoPath = 'ipfs-1111' //+ Math.random()
            const ipfs = new Ipfs({ repo: repoPath })

            ipfs.on('ready', () => {
                const files = [
                {
                    path: 'image.png',
                    content: ipfs.types.Buffer.from(btoa(fr.result),"base64")
                }
                ]

                ipfs.files.add(files, function (err, files) {
                    let url = "https://ipfs.io/ipfs/"+files[0].hash;
                    log("Storing file on IPFS using Javascript. HASH: https://ipfs.io/ipfs/"+files[0].hash);
                    ipfsPath = files[0].hash
                    ipfs.files.cat(ipfsPath, function (err, file) {
                    if (err) {
                        throw err
                    }
                    img = file.toString("base64");
                    document.getElementById("design_display").src= "data:image/png;base64," + img;
                    })
                })
    
                const log = (line) => {
                    document.getElementById('output').appendChild(document.createTextNode(`${line}\r\n`))
                }
    
    
            })
        }


    }
    
    
    function loadImage() {
        var input, file, fr;

        input = document.getElementById('inputtext').value;
            
            //IPFS START
            const repoPath = 'ipfs-1111' //+ Math.random()
            const ipfs = new Ipfs({ repo: repoPath })

            ipfs.on('ready', () => {

                    ipfsPath = input
                    ipfs.files.cat(ipfsPath, function (err, file) {
                    if (err) {
                        throw err
                    }
                    img = file.toString("base64");
                    document.getElementById("design_display").src= "data:image/png;base64," + img;
                    })
    
                const log = (line) => {
                    document.getElementById('output').appendChild(document.createTextNode(`${line}\r\n`))
                }
    
    
            })
        


    }

By MrAddon

Posted by:.

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

  1. I implemented the above functionality to upload image file to IPFS. But I get “TypeError: Cannot read property ‘Buffer’ of undefined”. Do I need to install any dependencies for it?

    Liked by 1 person

  2. I get “TypeError: Cannot read property ‘Buffer’ of undefined” when trying to implement the above functions. Do I need to install any dependencies?

    const ipfs = new Ipfs({ repo: String(Math.random() + Date.now()) } );

    ipfs.on(‘ready’, () => {
    console.log(‘Online status: ‘, ipfs.isOnline() ? ‘online’ : ‘offline’)
    const files = [
    {
    path: fName,
    content: ipfs.types.Buffer.from(btoa(reader.result),”base64″)
    }
    ]

    ipfs.add(files, function (err, files) {

    console.log(“Storing file on IPFS using Javascript. HASH: https://ipfs.io/ipfs/“+files[0].hash);

    });
    });

    Like

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