javascripthyperledgerhyperledger-composeribm-blockchain

How to upload image on hyperledger composer playground?


I am trying to build a Block chain application for distributed image sharing and copyright protection. I am using image as an asset.

So now I want to upload an image on Hyper ledger Composer playground. How can I do that?


Solution

  • You can store your file data into the IPFS. IPFS is a protocol and network designed to create a content-addressable, peer-to-peer method of storing and sharing hypermedia in a distributed file system.

    For IPFS I recommend you to follow the link

    In your application, In js file where you need to store Image. There you have to just write ipfs connectivity code. When you run the application at that time just make sure ipfs daemon started.

    IPFS will give you a Hash link after successfully upload a file. You can store that hash into an asset or participate of hyperledger composer.

    for example

    function toIPFS(file) {
        return new Promise(resolve => {
            const reader = new FileReader();
            reader.onloadend = function() {
            const ipfs = window.IpfsApi('ipfs', 5001,{protocol : "https"}) // Connect to IPFS
            const buf = buffer.Buffer(reader.result) // Convert data into buffer
            ipfs.files.add(buf, (err, result) => { // Upload buffer to IPFS
                if(err) {
                  return
                }
                 let url = `https://ipfs.io/ipfs/${result[0].hash}`
                  resolve('resolved url');
              })
            }
            reader.readAsArrayBuffer(file); // Read Provided File
       });
      }
    

    I hope it will help you. :)