Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seeding a file on Browser #904

Closed
ritwickdsouza opened this issue Sep 6, 2016 · 2 comments
Closed

Seeding a file on Browser #904

ritwickdsouza opened this issue Sep 6, 2016 · 2 comments

Comments

@ritwickdsouza
Copy link

@ritwickdsouza ritwickdsouza commented Sep 6, 2016

  • WebTorrent version: latest
  • Node.js version: v4.2.6
  • Browser name/version (if using WebTorrent in the browser): Chromium
<html>
  <head>
    <title>Instant.io Clone</title>
  <body>
    <div>
      <h1> Add Files to Seed </h1>
      <form>
        Select a file:<input id="seed_file" type="file"><br>
        <button id="start_seed" type="button">Start Seeding</button>
        <p class="seed_log"></p>
      </form>
    </div>
    <script src="webtorrent.min.js" type="text/javascript"></script>
    <script src="app.js" type="text/javascript"></script>
  </body>
</html>

Index.html

var client = new WebTorrent()

document.getElementById('start_seed').addEventListener('click', function (e) {
  e.preventDefault()
  var file = String(document.getElementById('seed_file'))
  if (file.length === 0) return 0
  client.seed(file, onTorrent)
})

function onTorrent (torrent) {
  console.log('Torrent info hash: ' + torrent.infoHash)
  torrent.files.forEach(function (file) {
    file.createReadStream().pipe(concat(function (buf) {
      var a = document.createElement('a')
      a.download = file.name
      a.href = URL.createObjectURL(new Blob([ buf ]))
      a.textContent = 'download ' + file.name
      document.body.appendChild(a)
    }))
  })
}

app.js

When i select a file and start seeding it, i get the following error.
Uncaught Error: filesystem paths do not work in the browser
I understand that that type of the variable file is incorrect. What do i need to do to fix it ?

@ritwickdsouza

This comment has been minimized.

Copy link
Author

@ritwickdsouza ritwickdsouza commented Sep 9, 2016

We need to pass File instance:
document.getElementById('seed_file').addEventListener('change', handleFileSelect, false)

function handleFileSelect(ev) {
  ev.preventDefault()
  client.seed(this.files[0])
}
@lock

This comment has been minimized.

Copy link

@lock lock bot commented May 4, 2018

This thread has been automatically locked because it has not had recent activity. To discuss futher, please open a new issue.

@lock lock bot locked as resolved and limited conversation to collaborators May 4, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.