【发布时间】:2021-12-27 02:20:44
【问题描述】:
我不擅长 javascript。我正在尝试通过修改此示例将一些文件作为文件夹上传到 nft.storage -> https://github.com/nftstorage/nft.storage/blob/main/packages/client/examples/node.js/storeDirectory.js
我的文件不是通过表单上传,而是存储在我的 PC 文件系统中。下面是我的js代码。
<script type="module">
import { NFTStorage, File } from 'https://cdn.skypack.dev/nft.storage'
import { fs } from 'https://cdn.skypack.dev/fs-'
const endpoint = 'https://api.nft.storage' // the default
const token = 'RDA0NjI0MTIzMTA2Mzgy....' // your API key from https://nft.storage/manage
function log(msg) {
msg = JSON.stringify(msg, null, 2)
document.getElementById('out').innerHTML += `${msg}\n`
}
document.querySelector('#upload-file-ipfs').addEventListener('click', async (e) => {
e.preventDefault()
const storage = new NFTStorage({ endpoint, token })
const cid = await storage.storeDirectory([
new File([await fs.promises.readFile('metadata.json')], 'metadata.json'),
new File([await fs.promises.readFile('test.png')], 'test.png'),
])
console.log({ cid })
const status = await storage.status(cid)
console.log(status)
})
</script>
但我在下面不断收到此错误。
Uncaught SyntaxError: The requested module 'https://cdn.skypack.dev/fs-' does not provide an export named 'fs'
我尝试用“https://www.skypack.dev/view/fs-extra”和“https://cdn.skypack.dev”替换“https://cdn.skypack.dev/fs-” /graceful-fs' 它给了我同样的错误。
如果我删除“fs”周围的花括号,我会得到Uncaught Error: [Package Error] "fs" does not exist. (Imported by "fs-").
非常感谢任何帮助。我的应用程序是 PHP+ JS,而不是 node.js。
【问题讨论】:
-
@CherryDT 谢谢。好吧,我根本不擅长 js,而且我的应用程序不是基于 node,js,它是 PHP + JS。那么有没有办法让我在浏览器上使用 fs 运行相同的代码?
-
请仅使用与当前问题相关的标签来标记您的问题。由于此处不涉及 PHP,因此您应该删除该标签。 node.js 也是如此,因为您明确表示您的应用不是 node.js 应用。
标签: javascript node.js ipfs skypack-cdn