Add buttons to download specific files
parent
f64ff1b030
commit
8129cc0753
|
|
@ -2,12 +2,28 @@ import React, { Component, Fragment } from 'react';
|
|||
import WebTorrent from 'webtorrent';
|
||||
import { saveAs } from 'file-saver/FileSaver';
|
||||
|
||||
const saveFile = (file) =>
|
||||
{
|
||||
file.getBlob((err, blob) =>
|
||||
{
|
||||
if (err)
|
||||
{
|
||||
console.error('WebTorrent error');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('TRYING TO SAVE BLOB', blob)
|
||||
saveAs(blob, file.name);
|
||||
});
|
||||
};
|
||||
|
||||
class FileChatEntry extends Component
|
||||
{
|
||||
state = {
|
||||
active: false,
|
||||
numPeers: 0,
|
||||
progress: 0
|
||||
progress: 0,
|
||||
files: null
|
||||
};
|
||||
|
||||
download = () =>
|
||||
|
|
@ -31,17 +47,10 @@ class FileChatEntry extends Component
|
|||
|
||||
torrent.on('done', () => {
|
||||
onProgress();
|
||||
clearInterval(onProgress);
|
||||
|
||||
torrent.files.forEach((file) => {
|
||||
file.getBlob((err, blob) => {
|
||||
if (err)
|
||||
{
|
||||
console.error('webtorrent error!!!');
|
||||
return;
|
||||
}
|
||||
|
||||
saveAs(blob);
|
||||
});
|
||||
this.setState({
|
||||
files: torrent.files
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -62,6 +71,16 @@ class FileChatEntry extends Component
|
|||
progress: {this.state.progress}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{this.state.files && (
|
||||
<div>
|
||||
{this.state.files.map((file, i) => (
|
||||
<div key={i}>
|
||||
<button onClick={() => saveFile(file)}>download {file.name}</button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</Fragment>
|
||||
);
|
||||
|
|
|
|||
Loading…
Reference in New Issue