【发布时间】:2019-08-29 09:34:30
【问题描述】:
我正在根据js-ipfs-http-client浏览器示例here做一些研究和测试
当我尝试从 IPFS 获得响应时,我从 Firefox 控制台收到以下警告:
跨域请求被阻止:同源策略不允许读取位于http://localhost:5001/api/v0/add?wrapWithDirectory=true&progress=true&wrap-with-directory=true&stream-channels=true 的远程资源。 (原因:CORS 标头“Access-Control-Allow-Origin”缺失)。
我已经尝试了建议的(肮脏)修复,您可以从终端更改 IPFS 配置:
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\"*\"]"
但即使这样似乎也不起作用。我开始研究可能使用提到的自定义标头here
运气不好。
我这样设置 IPFS: const ipfsClient = require('ipfs-http-client'); var ipfs = ipfsClient('localhost', '5001');
然后,一旦我从用户那里获得了一些文件,我就会尝试将其保存到 IPFS,如下所示:
function saveToIpfsWithFilename (file)
{
console.log('running save');
let ipfsId;
const fileStream = fileReaderPullStream(file);
const fileDetails =
{
path: file.name,
content: fileStream
};
const options =
{
wrapWithDirectory: true,
progress: (prog) => console.log(`received: ${prog}`)
};
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)
}).catch((err) => {
console.error(err)
});
}
【问题讨论】:
-
嗯,我在尝试运行示例以进行测试时遇到错误。报告了这个问题,一旦我得到它的工作将在这里查看。
-
好的,修复了示例 [1] 并在文档 [2] 中记录了两个配置更改,它对我有用。我正在运行具有 go-ipfs 0.4.19 的 Firefox Nightly 68 和 IPFS Desktop 0.7.2。 (1.github.com/ipfs/js-ipfs-http-client/pull/970 2.github.com/ipfs/js-ipfs-http-client/tree/master/examples/…)
标签: javascript node.js browserify ipfs