diff --git a/examples/browser-add/.gitignore b/examples/browser-add/.gitignore new file mode 100644 index 000000000..4187d6774 --- /dev/null +++ b/examples/browser-add/.gitignore @@ -0,0 +1 @@ +bundle.js \ No newline at end of file diff --git a/examples/browser-add/README.md b/examples/browser-add/README.md new file mode 100644 index 000000000..c052a5723 --- /dev/null +++ b/examples/browser-add/README.md @@ -0,0 +1,18 @@ +# JS IPFS API - Example Browser - Add + +## Setup + +Install [go-ipfs](https://ipfs.io/docs/install/) and run it + +```bash +$ ipfs daemon +``` + +then in this folder run + +```bash +$ npm install +$ npm start +``` + +and open your browser at `http://localhost:8888` diff --git a/examples/browser-add/index.html b/examples/browser-add/index.html new file mode 100644 index 000000000..d92470205 --- /dev/null +++ b/examples/browser-add/index.html @@ -0,0 +1,18 @@ + + + + + JS IPFS API - Example - Browser - Add + + + +

JS IPFS API - Add file from the browser

+ + +
found in ipfs:
+
[ipfs hash]
+
[ipfs content]
+
+ + diff --git a/examples/browser-add/index.js b/examples/browser-add/index.js new file mode 100644 index 000000000..6765b61ff --- /dev/null +++ b/examples/browser-add/index.js @@ -0,0 +1,41 @@ +var IPFS = require('ipfs-api') +var ipfs = IPFS() + +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, stream) { + if(err || !stream) return console.error("ipfs cat error", err, stream); + var res = ''; + + if(!stream.readable) { + console.error('unhandled: cat result is a pipe', stream); + } else { + stream.on('data', function (chunk) { + res += chunk.toString(); + }) + + stream.on('error', function (err) { + console.error('Oh nooo', err); + }) + + stream.on('end', function () { + document.getElementById('hash').innerText = hash; + document.getElementById('content').innerText = res; + }) + } + }); +} + +document.getElementById('store').onclick=store; diff --git a/examples/browser-add/package.json b/examples/browser-add/package.json new file mode 100644 index 000000000..a2c8dbcfb --- /dev/null +++ b/examples/browser-add/package.json @@ -0,0 +1,18 @@ +{ + "name": "ipfs-api-example-browser-add", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "start": "browserify -t brfs index.js > bundle.js && http-server -a 127.0.0.1 -p 8888" + }, + "keywords": [], + "author": "Friedel Ziegelmayer", + "license": "MIT", + "devDependencies": { + "brfs": "^1.4.3", + "browserify": "^13.0.1", + "http-server": "^0.9.0", + "ipfs-api": "^6.0.3" + } +}