-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
80 lines (72 loc) · 2.25 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<html>
<body>
<h1>IPFS Chat</h1>
<div id="status">Connecting...</div>
<div>
<input id="message"/><button onclick="create()">Create</button>
<div id="output"></div>
</div>
<div>
<input id="hash"/><button onclick="submit()">Submit</button><br>
<div id="outputName"></div>
</div>
<input type="file" id="myimage"/><button onclick="addimage()">Add Image</button><br>
<div id="imagehash"></div>
<div>
<h3>Load Image</h3>
<input type="text" id="loadimage"/><button onclick="loadimage()">Load Image</button>
<img src="" id="imageid"/>
</div>
<script src="https://cdn.jsdelivr.net/npm/ipfs@0.31.7/dist/index.min.js"></script>
<script>
const node = new Ipfs({})
node.on('ready', () => {
console.log('ready')
document.querySelector('#status').innerHTML = 'Ready'
})
function create() {
let value = document.querySelector('#message').value
node.files.add({
content: node.types.Buffer(value)
}, (err, res) => {
console.log({err, res})
document.querySelector("#output").innerHTML=res[0].hash
})
}
function submit() {
let value = document.querySelector('#hash').value
node.files.get(
value
, (err, res) => {
console.log({err, res})
console.log(res[0].content)
})
}
function addimage() {
let file = document.querySelector("#myimage").files[0]
console.log(file)
let reader = new FileReader()
reader.onloadend = function() {
console.log(reader.result)
node.files.add({
content: node.types.Buffer.from(reader.result)
}, (err, res) => {
console.log({err, res})
document.querySelector("#imagehash").innerHTML=res[0].hash
})
}
reader.readAsArrayBuffer(file)
}
function loadimage() {
let value = document.querySelector('#loadimage').value
node.files.get(value,(err, res) => {
console.log(res[0].content)
let blob = new Blob([res[0].content], {type : 'image/jpg'})
let imageUrl = URL.createObjectURL(blob)
document.querySelector('#imageid').src=imageUrl
}
)
}
</script>
</body>
</html>