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

Commit

Permalink
examples: add browser-add-example
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Aug 4, 2016
1 parent 9d17e1e commit 0a5fbbf
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/browser-add/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bundle.js
18 changes: 18 additions & 0 deletions examples/browser-add/README.md
Original file line number Diff line number Diff line change
@@ -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`
18 changes: 18 additions & 0 deletions examples/browser-add/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>JS IPFS API - Example - Browser - Add</title>
<script src="bundle.js"></script>
</head>
<body>
<h1>JS IPFS API - Add file from the browser</h1>
<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>
</body>
</html>
34 changes: 34 additions & 0 deletions examples/browser-add/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'

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

function store () {
var toStore = document.getElementById('source').value
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
18 changes: 18 additions & 0 deletions examples/browser-add/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}

0 comments on commit 0a5fbbf

Please sign in to comment.