Skip to content
This repository was archived by the owner on Aug 12, 2020. It is now read-only.
This repository was archived by the owner on Aug 12, 2020. It is now read-only.

New Example: create and read ipfs files in browser #17

Open
@carver

Description

@carver

Note to self. Something like:

Write this to index.js

var ipfs = require('ipfs-api')();

function store() {
  var toStore = document.getElementById('source').value;
  //TODO un-break this call:
  ipfs.add(new Buffer(toStore), function (err, res){
    if(err || !res) return console.error("ipfs add error", err, res);

    res.forEach(function(file) {
      console.log('successfully stored', file.Hash);
      display(file.Hash); 
    });
  });
}

function display(hash) {
  ipfs.cat(hash, function(err, res) {
    if(err || !res) return console.error("ipfs cat error", err, res);
    if(res.readable) {
      console.error('unhandled: cat result is a pipe', res);
    } else {
      document.getElementById('hash').innerText=hash;
      document.getElementById('content').innerText=res;
    }
  });
}

document.getElementById('store').onclick=store;
#sloppy html demo
echo '<textarea id="source"></textarea>
<button id="store">create in ipfs</button>
<div><div>found in ipfs:</div>
<div id="hash">[ipfs hash]</div>
<div id="content">[ipfs content]</div></div>
<script type="text/javascript" src="ipfs.js"></script>' > index.html

#file server
sudo npm install -g browserify http-server
npm install ipfs-api
browserify index.js > ipfs.js #do this every time you change index.js, or better: set up grunt watch
http-server -a 127.0.0.1 -p 8888

#ipfs server
export API_ORIGIN="http://localhost:8888"
ipfs daemon

Open http://localhost:8888/index.html

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions