-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact_file_template.js
62 lines (55 loc) · 1.6 KB
/
react_file_template.js
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
'use strict'
const React = require('react')
const ipfsClient = require('ipfs-http-client')
const fileReaderPullStream = require('pull-file-reader')
class App extends React.Component {
constructor () {
super()
this.state = {
added_file_hash: null
}
this.ipfs = ipfsClient('localhost', '5001')
this.captureFile = this.captureFile.bind(this)
this.saveToIpfs = this.saveToIpfs.bind(this)
this.handleSubmit = this.handleSubmit.bind(this)
}
captureFile (event) {
event.stopPropagation()
event.preventDefault()
const file = event.target.files[0]
this.saveToIpfs(file)
}
saveToIpfs (file) {
let ipfsId
const fileStream = fileReaderPullStream(file)
this.ipfs.add(fileStream, { progress: (prog) => console.log(`received: ${prog}`) })
.then((response) => {
console.log(response)
ipfsId = response[0].hash
console.log(ipfsId)
this.setState({added_file_hash: ipfsId})
}).catch((err) => {
console.error(err)
})
}
handleSubmit (event) {
event.preventDefault()
}
render () {
return (
<div>
<form id='captureMedia' onSubmit={this.handleSubmit}>
<input type='file' onChange={this.captureFile} /><br/>
<label htmlFor='keepFilename'><input type='checkbox' id='keepFilename' name='keepFilename' /> keep filename</label>
</form>
<div>
<a target='_blank'
href={'https://ipfs.io/ipfs/' + this.state.added_file_hash}>
{this.state.added_file_hash}
</a>
</div>
</div>
)
}
}
module.exports = App