Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit efcb392

Browse files
committed
Add sketch of the tutorial
1 parent 90befbf commit efcb392

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

examples/interop-examples/README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,93 @@ npm start
1010
```
1111

1212
Open http://127.0.0.1:8080 in your favorite browser.
13+
14+
## Tutorial
15+
16+
Steps
17+
1. create IPFS instance
18+
19+
First, we need an IPFS instance that we'll use throughout the tutorial. For the sake of convinience, we'll use `ipfs-daemon` module to create and IPFS instance.
20+
21+
Add `ipfs-daemon` to your project:
22+
```
23+
npm i ipfs-daemon --save
24+
```
25+
26+
Add `ipfs-daemon` to your html:
27+
```html
28+
<script type="text/javascript" src="node_modules/ipfs-daemon/dist/ipfs-browser-daemon.js" charset="utf-8"></script>
29+
```
30+
31+
Create the IPFS instance and wait for it to start:
32+
33+
```javascript
34+
const ipfs = new IpfsDaemon({
35+
// Directory to which save IPFS data to
36+
IpfsDataDir: '/path/to/ipfs/data',
37+
// IPFS dev server: webrtc-star-signalling.cloud.ipfs.team
38+
SignalServer: '188.166.203.82:20000',
39+
})
40+
41+
ipfs.on('error', (e) => console.error(e))
42+
ipfs.on('ready', () => {
43+
// We're ready to use IPFS!
44+
})
45+
```
46+
47+
2. listen for file drop
48+
49+
```html
50+
<div id="ipfs" class="ipfs" ondragover="event.preventDefault()"></div>
51+
```
52+
53+
```javascript
54+
const rootElement = document.getElementById("ipfs")
55+
rootElement.addEventListener('drop', onDrop)
56+
```
57+
58+
```javascript
59+
const onDrop = (event) => {
60+
event.preventDefault()
61+
const files = event.dataTransfer.files
62+
63+
// Go through each file (allows dropping multiple files)
64+
for (let i = 0; i < files.length; i ++) {
65+
const file = files[i]
66+
// See details below for reading file contents
67+
readFileContents(file)
68+
.then((content) => {
69+
// Add the file to IPFS
70+
// See detailed documentation at: https://github.com/ipfs/interface-ipfs-core/tree/master/API/files#javascript---ipfsfilesadddata-options-callback
71+
return ipfs.files.add([{
72+
path: file.name,
73+
content: new ipfs.types.Buffer(content)
74+
}])
75+
})
76+
.then((files) => {
77+
// Output the added files and their hashes to the HTML
78+
rootElement.innerHTML = files
79+
.map((e) => `Added ${e.path} as ${e.hash}`)
80+
.join('<br>')
81+
})
82+
.catch(onError)
83+
}
84+
}
85+
```
86+
87+
The function to read file's contents:
88+
89+
```javascript
90+
const readFileContents = (file) => {
91+
return new Promise((resolve) => {
92+
const reader = new FileReader()
93+
reader.onload = (event) => resolve(event.target.result)
94+
reader.readAsArrayBuffer(file)
95+
})
96+
}
97+
```
98+
99+
3. add dropped file to IPFS
100+
4. output file's hash
101+
5. ipfs.files.cat
102+
6. output to <img>

0 commit comments

Comments
 (0)