【发布时间】:2021-12-18 19:15:10
【问题描述】:
我试图从我的 react js 前端应用程序将图像上传到 pinata ipfs 云。但它向我显示错误:Unhandled Rejection (TypeError): fs.createReadStream is not a function。 .
这是我的代码 sn-p:
require('dotenv').config();
const fs = require('fs');
const axios = require('axios');
const FormData = require('form-data');
const key = process.env.REACT_APP_PINATA_KEY;
const secret = process.env.REACT_APP_PINATA_SECRET;
export const pinFileToIPFS = async (filePath) => {
const url = "https://api.pinata.cloud/pinning/pinFileToIPFS";
//we gather a local file for this example, but any valid readStream source will work here.
let data = new FormData();
data.append('file',fs.createReadStream(filePath));
return axios.post(url,
data,
{
maxContentLength: 'Infinity', //this is needed to prevent axios from erroring out with large files
headers: {
'Content-Type': `multipart/form-data; boundary=${data._boundary}`,
'pinata_api_key': key,
'pinata_secret_api_key': secret
}
})
.then(function (response) {
//handle response here
return {
success: true,
pinataUrl: "https://gateway.pinata.cloud/ipfs/" + response.data.IpfsHash
};
})
.catch(function (error) {
//handle error here
console.log(error)
return {
success: false,
message: error.message,
}
});
};
如何解决它或者有什么方法可以做到这一点而不是使用'fs'?
【问题讨论】: