欢迎来到BTFS开发者指南,在这里您将找到全面的指南和文档。BitTorrent文件系统(BTFS)既是一个协议,也是一种网络应用,它提供了一种内容可寻址的点对点机制。
介绍
BTFS当前与IPFS兼容相同的JavaScript和Go SDK库。本指南将引导用户通过一个简单的示例通过SDK集成BTFS网络和TRON智能合约。
实例
假设您基于Web的DApp的HTML主页面引用了后端JS文件:
<script type="text/javascript" src="backend.js?2019.06.03.v2"> </script>To implement a function adding files to BTFS, you can first define the IP address and accessible port of your BTFS node.
const ipAddr = "api.btfs.trongrid.io";const ipPort = "443";Then define a variable with the host IP, port number, and protocol.
const btfs = window.IpfsApi({host:ipAddr, port:ipPort, protocol:'https'});add方法的形式为.add(data,[options],[callback]),其中数据可以是:
可读流缓冲区实例一份文件拉流对象数组,每个对象采用以下形式:{ path: '/tmp/myfile.txt', // The file path content: <data> // A Buffer, Readable Stream, Pull Stream or File with the contents of the file}
// backend.js
const ipAddr = "api.btfs.trongrid.io";const ipPort = "443";
async function add() { const btfs = window.IpfsApi({host:ipAddr, port:ipPort, protocol:'https'}); const Buffer = window.IpfsApi().Buffer; const newReader = new FileReader(); var movieHash, coverHash; newReader.onloadend = function () { const buf = Buffer.from(newReader.result); btfs.files.add(buf, (err, result) => { if(err) { console.error(err); return } let url = `https://yourURL./btfs/${result[0].hash}`; console.log(`Url --> ${url}`); document.getElementById("url").innerHTML= url; document.getElementById("url").href= url; document.getElementById("url").target = '_blank'; }); }; const DApp = document.getElementById("DApp"); newReader.readAsArrayBuffer(DApp.files[0]);}
资源IPFS JS SDK存储库-用JavaScript实现的IPFS HTTP API的客户端库。