gpt4 book ai didi

javascript - 使用 ipfs (js-ipfs-http-client) 将完整目录上传到 IPFS

转载 作者:太空宇宙 更新时间:2023-11-03 23:55:28 32 4
gpt4 key购买 nike

我想使用 (js-ipfs-http-client) 模块将目录上传到浏览器上的 ipfs。

我发现了这个老问题。 https://github.com/ipfs/js-ipfs/issues/277所以我决定使用递归的方式来添加文件并只获取它的一个哈希值。

ipfs.addFromFs('path', { recursive: true, ignore: ['subfolder/to/ignore/**'] }, (err, result) => {
if (err) { throw err }
console.log(result)
})

但是它给了我这个错误。 fs Add doesn't work on browser

我需要使用 javascript 将目录上传到 ipfs,但我找到的所有资源只上传一个文件。或者很多带有哈希值数组的文件。我需要一种方法来上传目录的所有文件并仅使用一个哈希值来获取文件。提前致谢。

最佳答案

叶海亚,我相信您正在寻找的答案就在 https://github.com/ipfs/js-ipfs-http-client/blob/master/examples/upload-file-via-browser/src/App.js#L29-L67

// Example #1
// Add file to IPFS and return a CID
saveToIpfs (files) {
let ipfsId
this.ipfs.add([...files], { progress: (prog) => console.log(`received: ${prog}`) })
.then((response) => {
console.log(response)
ipfsId = response[0].hash
console.log(ipfsId)
this.setState({ added_file_hash: ipfsId })
}).catch((err) => {
console.error(err)
})
}

// Example #2
// Add file to IPFS and wrap it in a directory to keep the original filename
saveToIpfsWithFilename (files) {
const file = [...files][0]
let ipfsId
const fileDetails = {
path: file.name,
content: file
}
const options = {
wrapWithDirectory: true,
progress: (prog) => console.log(`received: ${prog}`)
}
this.ipfs.add(fileDetails, options)
.then((response) => {
console.log(response)
// CID of wrapping directory is returned last
ipfsId = response[response.length - 1].hash
console.log(ipfsId)
this.setState({ added_file_hash: ipfsId })
}).catch((err) => {
console.error(err)
})
}

关于javascript - 使用 ipfs (js-ipfs-http-client) 将完整目录上传到 IPFS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57413598/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com