diff --git a/examples/browser-add-file/README.md b/examples/browser-add-file/README.md new file mode 100644 index 000000000..c052a5723 --- /dev/null +++ b/examples/browser-add-file/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-file/index.html b/examples/browser-add-file/index.html new file mode 100644 index 000000000..bf1910ee8 --- /dev/null +++ b/examples/browser-add-file/index.html @@ -0,0 +1,28 @@ + + + + + JS IPFS API - Example - Browser - Add + + + + + +
+

Add text - works

+ + +
+
+

Add buffer - does not work

+ + +
+
+

Response from ipfs - stream res unhandled

+
[ipfs hash]
+
[ipfs content]
+
+ + diff --git a/examples/browser-add-file/index.js b/examples/browser-add-file/index.js new file mode 100644 index 000000000..100a9ca76 --- /dev/null +++ b/examples/browser-add-file/index.js @@ -0,0 +1,50 @@ +/* globals FileReader */ +'use strict' + +var ipfs = window.IpfsApi() + +function storeText () { + store(document.getElementById('source').value) +} + +function storeFile () { + const file = document.getElementById('filePicker').files[0] + const reader = new FileReader() + reader.onload = function () { + store(reader.result) + } + reader.readAsArrayBuffer(file) +} + +// from examples/browser-add +function store (toStore) { + ipfs.add(new window.buffer.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) + display(file.path) + }) + }) +} + +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.addEventListener('DOMContentLoaded', function () { + document.getElementById('storeText').onclick = storeText + document.getElementById('storeBuffer').onclick = storeFile +}) diff --git a/examples/browser-add-file/package.json b/examples/browser-add-file/package.json new file mode 100644 index 000000000..56dd07187 --- /dev/null +++ b/examples/browser-add-file/package.json @@ -0,0 +1,16 @@ +{ + "name": "ipfs-api-example-browser-add-file", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "start": "http-server -a 127.0.0.1 -p 8888" + }, + "keywords": [], + "author": "nycoliver", + "license": "MIT", + "devDependencies": { + "http-server": "^0.9.0", + "ipfs-api": "^6.0.3" + } +}