Previous posts:
- How to push/load image file from/to IPFS using Javascript. Examples (PART I)
- How to push/load image file from/to IPFS using Javascript. Examples (PART II)
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)
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
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
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?
LikeLiked by 1 person
You must connect to an IPFS server. Try with this post: https://mraddon.blog/2019/04/29/html-and-javascript-code-to-load-a-file-in-your-ipfs-server/
LikeLike
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);
});
});
LikeLike
Have you tried with this link?
view-source:http://www.ankonan.io/demo/ipfs/index.html
The script src tags of the header are the dependencies
Just change var ipfs = window.IpfsApi(‘ipfs.coinmarketrank.io’, ’80’) to your IPFS server
Regards
LikeLike
What is the purpouse of buffer on file or image?
LikeLike