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

Commit 0a5fbbf

Browse files
examples: add browser-add-example
1 parent 9d17e1e commit 0a5fbbf

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed

examples/browser-add/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bundle.js

examples/browser-add/README.md

+18
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

+18
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

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

examples/browser-add/package.json

+18
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)