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

Commit e2b36e8

Browse files
examples: add browser-add-example
1 parent 848cb87 commit e2b36e8

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

examples/browser-add/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bundle.js

examples/browser-add/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# JS IPFS API - Example Browser - Add
2+
3+
## Setup
4+
5+
Install [go-ipfs](https://ipfs.io/docs/install/) and run it
6+
7+
```bash
8+
$ ipfs daemon
9+
```
10+
11+
then in this folder run
12+
13+
```bash
14+
$ npm install
15+
$ npm start
16+
```
17+
18+
and open your browser at `http://localhost:8888`

examples/browser-add/index.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8"/>
5+
<title>JS IPFS API - Example - Browser - Add</title>
6+
<script src="bundle.js"></script>
7+
</head>
8+
<body>
9+
<h1>JS IPFS API - Add file from the browser</h1>
10+
<textarea id="source">
11+
</textarea>
12+
<button id="store">create in ipfs</button>
13+
<div><div>found in ipfs:</div>
14+
<div id="hash">[ipfs hash]</div>
15+
<div id="content">[ipfs content]</div>
16+
</div>
17+
</body>
18+
</html>

examples/browser-add/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var IPFS = require('ipfs-api')
2+
var ipfs = IPFS()
3+
4+
function store () {
5+
var toStore = document.getElementById('source').value
6+
//TODO un-break this call:
7+
ipfs.add(new Buffer(toStore), function (err, res) {
8+
if(err || !res) return console.error("ipfs add error", err, res)
9+
10+
res.forEach(function (file) {
11+
console.log('successfully stored', file.Hash);
12+
display(file.Hash);
13+
})
14+
})
15+
}
16+
17+
function display (hash) {
18+
ipfs.cat(hash, function(err, res) {
19+
if(err || !res) return console.error("ipfs cat error", err, res);
20+
if(res.readable) {
21+
console.error('unhandled: cat result is a pipe', res);
22+
} else {
23+
document.getElementById('hash').innerText=hash;
24+
document.getElementById('content').innerText=res;
25+
}
26+
});
27+
}
28+
29+
document.getElementById('store').onclick=store;

examples/browser-add/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "ipfs-api-example-browser-add",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "browserify -t brfs index.js > bundle.js && http-server -a 127.0.0.1 -p 8888"
8+
},
9+
"keywords": [],
10+
"author": "Friedel Ziegelmayer",
11+
"license": "MIT",
12+
"devDependencies": {
13+
"brfs": "^1.4.3",
14+
"browserify": "^13.0.1",
15+
"http-server": "^0.9.0",
16+
"ipfs-api": "^6.0.3"
17+
}
18+
}

0 commit comments

Comments
 (0)