Skip to content
This repository has been 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 opened this issue Jul 3, 2015 · 15 comments
Open

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

carver opened this issue Jul 3, 2015 · 15 comments

Comments

@carver
Copy link

carver commented Jul 3, 2015

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

@aniket-kumar
Copy link

I am getting an error with it : fs.readFileSync is not a function. what to do?

@aniket-kumar
Copy link

aniket-kumar commented Jun 10, 2016

@carver any help will be appreciated.

@carver
Copy link
Author

carver commented Jun 11, 2016

It's been a while since I've looked at this. Based on my fuzzy memory, there were some security issues preventing this from going live. The code above (untested at the time, and stale by a year now) was meant to be more of a reminder of where to start when writing a doc. It wasn't ready to be an example that people can use out of the box. I'm afraid I don't have time to dedicate to making the doc "production ready" right now. Getting it working will probably require talking to the ipfs team and/or digging deeper into the ipfs code.

@aniket-kumar
Copy link

ok

@dignifiedquire
Copy link

I've just created a working example based of this: ipfs-inactive/js-ipfs-http-client#315

@ColeMorton
Copy link

@dignifiedquire I've tried your example but I encounter this issue:
screen shot 2016-07-17 at 15 51 58
Any ideas would be great, thanks!

@aniket-kumar
Copy link

I think this error is due to the second check on the response in the display() function. I hope that there should be a replacement of "if(!res.readable)" at "if(res.readable)". Because if it is readable, it should not throw any error. Just give it a look as i didn't write the code, i am not very sure.

@ColeMorton
Copy link

@aniket-kumar thanks. Am I right to assume that the text I stored in IPFS (ghi) should be displayed in place of [ipfs content]? Currently, it displays Object object.
Is it possible to find the actual content (ghi)?

screen shot 2016-07-18 at 11 21 18

@aniket-kumar
Copy link

@ColeMorton I think you have to use JSON.parse() for that. Otherwise you can run loop to check every key of the object.

@ColeMorton
Copy link

@aniket-kumar thanks, I found the answer: ipfs-inactive/js-ipfs-http-client#55.

ipfs.cat('some hash', function (err, stream) {
  var res = ''

  stream.on('data', function (chunk) {
    res += chunk.toString()
  })

  stream.on('error', function (err) {
    console.error('Oh nooo', err)    
  })

  stream.on('end', function () {
    console.log('Got:', res)
  })
})

Thanks @dignifiedquire

@lindybrits
Copy link

lindybrits commented Oct 20, 2016

@aniket-kumar, what was the solution to your problem for -> "fs.readFileSync is not a function"? @dignifiedquire and @diasdavid, I am receiving this error message in the browser console. Any help would be greatly appreciated!

@JohnAllen
Copy link

When IPFS adds files in the browser where is the data written to? Browser memory or local storage and there's a fairly small limit?

@daviddias
Copy link
Contributor

@JohnAllen by default it is written to IndexedDB, however, that can be changed as a repo configuration.

@daviddias
Copy link
Contributor

@lindybrits I believe I answered your question through IRC at the time, but just for the record, the examples here should show you how to bundle js-ipfs-api for the browser https://github.com/ipfs/js-ipfs-api/tree/master/examples

@raineorshine
Copy link

In case this helps someone else, the buffer option helped me:

ipfs.cat(hash, { buffer: true }, (err, res) => {
  ...
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants