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

Commit 843e227

Browse files
committed
fix: get files now works
1 parent 31c6383 commit 843e227

File tree

1 file changed

+30
-26
lines changed
  • examples/transfer-files/complete/js

1 file changed

+30
-26
lines changed

examples/transfer-files/complete/js/app.js

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -80,41 +80,45 @@ const catFile = () => {
8080
if (multihash) {
8181
// Get a file or many files
8282
ipfs.files.get(multihash, (err, stream) => {
83-
if (err) onError(err)
83+
if (err) {
84+
onError(err)
85+
}
8486
console.log(stream)
87+
8588
// .get gives us a stream of files
8689
stream.on('data', (file) => {
87-
console.log(file)
90+
console.log('got file', file)
91+
8892
const buf = []
93+
8994
if (file.content) {
9095
// once we get a file, we also want to read the data for that file
91-
file.content.on('data', (data) => {
92-
console.log('file got data')
93-
buf.push(data)
94-
})
95-
file.content.on('end', (data) => {
96-
console.log('file got end')
97-
console.log(buf)
96+
file.content.on('data', (data) => buf.push(data))
97+
98+
file.content.on('end', () => {
99+
console.log('The buf', buf)
100+
101+
const content = new window.Blob(buf, {type: 'application/octet-binary'})
102+
const contentUrl = window.URL.createObjectURL(content)
103+
104+
const listItem = document.createElement('div')
105+
const link = document.createElement('a')
106+
link.setAttribute('href', contentUrl)
107+
link.setAttribute('download', multihash)
108+
const date = (new Date()).toLocaleTimeString()
109+
110+
link.innerText = date + ' - ' + multihash + ' - Size: ' + file.size
111+
const fileList = document.querySelector('.file-list')
112+
113+
listItem.appendChild(link)
114+
fileList.insertBefore(listItem, fileList.firstChild)
98115
})
99-
// TODO currently, can't grab content from file either in get...
100-
const downloadContent = window.btoa(window.unescape(window.encodeURIComponent('content of file')))
101-
const downloadLink = 'data:application/octet-stream;charset=utf-8;base64,' + downloadContent
102-
const listItem = document.createElement('div')
103-
const link = document.createElement('a')
104-
link.setAttribute('href', downloadLink)
105-
link.setAttribute('download', multihash)
106-
const date = (new Date()).toLocaleTimeString()
107-
108-
link.innerText = date + ' - ' + multihash + ' - Size: ' + file.size
109-
const fileList = document.querySelector('.file-list')
110-
111-
listItem.appendChild(link)
112-
fileList.insertBefore(listItem, fileList.firstChild)
116+
117+
file.content.resume()
113118
}
114119
})
115-
stream.on('end', () => {
116-
console.log('no more files')
117-
})
120+
121+
stream.on('end', () => console.log('no more files'))
118122
})
119123
}
120124
}

0 commit comments

Comments
 (0)