当前位置: 首页 > news >正文

【p2p、分布式,区块链笔记 Torrent】WebTorrent的上传和下载界面

上传 upload.html client.seed

<!DOCTYPE html>
<html lang="zh">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>文件上传与哈希值</title><script src="https://cdn.jsdelivr.net/npm/webtorrent/webtorrent.min.js"></script>
</head>
<body><h1>文件上传与哈希值</h1><input type="file" id="fileInput" multiple><button id="uploadButton">上传并获取哈希值</button><div id="hashOutput"></div><div id="magnetOutput"></div><script>const client = new WebTorrent();document.getElementById('uploadButton').onclick = function () {const fileInput = document.getElementById('fileInput');const files = fileInput.files;if (files.length === 0) {alert('请先选择一个文件。');return;}const torrentId = files[0].name; // 你可以选择使用文件名或自定义的 ID// 创建一个新的 torrent 并为其播种(Node.js)https://webtorrent.io/introclient.seed(files, { name: torrentId }, function (torrent) {const hash = torrent.infoHash; // 获取哈希值document.getElementById('hashOutput').innerText = '文件哈希: ' + hash ;document.getElementById('magnetOutput').innerText = '文件链接: ' + torrent.magnetURI;console.log('哈希值:', hash);console.log('Client is seeding ' + torrent.magnetURI)});};</script>
</body>
</html>

下载 download.html client.add

  • 下载界面的代码来源
<!doctype html>
<html><body><h1>Download files using the WebTorrent protocol (BitTorrent over WebRTC).</h1><form><label for="torrentId">Download from a magnet link: </label><input name="torrentId", placeholder="magnet:" value="magnet:?xt=urn:btih:08ada5a7a6183aae1e09d831df6748d566095a10&dn=Sintel&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fsintel.torrent"><button type="submit">Download</button></form><h2>Log</h2><div class="log"></div><!-- Include the latest version of WebTorrent --><script src="https://cdn.jsdelivr.net/npm/webtorrent@latest/webtorrent.min.js"></script><script>const client = new WebTorrent()client.on('error', function (err) {console.error('ERROR: ' + err.message)})document.querySelector('form').addEventListener('submit', function (e) {e.preventDefault() // Prevent page refreshconst torrentId = document.querySelector('form input[name=torrentId]').valuelog('Adding ' + torrentId)client.add(torrentId, onTorrent)})function onTorrent (torrent) {log('Got torrent metadata!')log('Torrent info hash: ' + torrent.infoHash + ' ' +'<a href="' + torrent.magnetURI + '" target="_blank">[Magnet URI]</a> ' +'<a href="' + torrent.torrentFileBlobURL + '" target="_blank" download="' + torrent.name + '.torrent">[Download .torrent]</a>')// Print out progress every 5 secondsconst interval = setInterval(function () {log('Progress: ' + (torrent.progress * 100).toFixed(1) + '%')}, 5000)torrent.on('done', function () {log('Progress: 100%')clearInterval(interval)})// Render all files into to the pagetorrent.files.forEach(function (file) {file.appendTo('.log') // !!Render a file into to the page!!!log('(Blob URLs only work if the file is loaded from a server. "http//localhost" works. "file://" does not.)')file.getBlobURL(function (err, url) {if (err) return log(err.message)log('File done.')log('<a href="' + url + '">Download full file: ' + file.name + '</a>')})})}function log (str) {const p = document.createElement('p')p.innerHTML = strdocument.querySelector('.log').appendChild(p)}</script></body>
</html>

效果

上传

在这里插入图片描述

下载

Client is seeding magnet:?xt=urn:btih:5fe7e4f9ab9d978560a95612d6c3fd2888bac963&dn=carbon.png&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com

CG

  • render-media:在浏览器中智能呈现媒体文件
  • 【p2p、分布式,区块链笔记 Torrent】: WebTorrent & GitTorrent & bittorrent-dht & bep0044

http://www.mrgr.cn/news/61371.html

相关文章:

  • 哥大开发AI模型助力癌症和遗传病研究,近屿智能专注培养AI人才
  • lerna使用指南
  • 【Linux】Linux基础命令(二)
  • 网络安全、Web安全、渗透测试之笔经面经总结
  • vue2修改表单只提交被修改的数据的字段传给后端接口
  • 【深度学习】布匹寻边:抓边误差小于3px【附完整链接】
  • 抽象类和接口的区别
  • PostgreSQL数据类型怎么选?
  • LiNo论文解析
  • 【每日刷题】Day145
  • 架构师备考-背诵精华(计算机语言)
  • Java Lock Condition 源码
  • 代码质量与项目进度的博弈
  • Homework 1 - Random Distribution Related
  • 手写ioc容器(简易版)
  • 【jvm】如何设置堆内存大小
  • 事务学习一
  • 年薪百万打工人自爆:我的大厂生存指南!
  • 使用DeepLabV3实现植叶病害检测
  • File类踩坑记录
  • 细胞核荧光探针(一):一种红色发光、NADPH响应的的喹啉基
  • 【点云异常点检测数据集】Real3D-AD数据集介绍
  • 基于SSM大学生互动交流网站设计与实现
  • 四元数各个旋转API的使用
  • 【JSON相关漏洞(Hijacking+Injection)挖掘技巧及实战案例全汇总】
  • mongo实操笔记