我只是四处闲逛,想办法把文件钉在皮纳塔身上。
虽然我有一种方法可以从服务器端执行此操作,但不知道如何从客户端(如React.js)这样做:
这是服务器端方法
async function storeImages(imagesFilePath) {
const fullImagesPath = path.resolve(imagesFilePath);
const files = fs.readdirSync(fullImagesPath);
let responses = [];
console.log("Uploading to Pinata !!!");
console.log("paths ", fs.createReadStream(fullImagesPath + "/" + files[0]));
for (fileIndex in files) {
const readableStreamForFile = fs.createReadStream(
`${fullImagesPath}/${files[fileIndex]}`
);
try {
const response = await pinata.pinFileToIPFS(readableStreamForFile);
responses.push(response);
} catch (error) {
console.log(error);
}
}
return { responses, files };
}
async function storeTokeUriMetadata(metadata) {
try {
const response = await pinata.pinJSONToIPFS(metadata);
return response;
} catch (error) {
console.log(error);
}
return null;
}皮纳塔主页:松属(带有一些手动上传文件的Pinata主页)
非常感谢你的帮助
发布于 2022-09-03 15:53:10
这是一种可以用于从客户端上传文件的方法,即使用react.js或next.js
const sendFileToIPFS = async (fileImg) => {
if (fileImg) {
try {
const formData = new FormData()
formData.append("file", fileImg)
const resFile = await axios({
method: "post",
url: "https://api.pinata.cloud/pinning/pinFileToIPFS",
data: formData,
headers: {
pinata_api_key: `${REACT_APP_PINATA_API_KEY}`,
pinata_secret_api_key: `${REACT_APP_PINATA_API_SECRET}`,
"Content-Type": "multipart/form-data",
},
})
setNftHash(resFile.data.IpfsHash)
setTrxLoading(true)
} catch (error) {
console.log("Error sending File to IPFS: ")
console.log(error)
}
}
}在表单的sendFilesToIpfs方法中调用此方法onSubmit
{
sendFileToIPFS
}}
>https://ethereum.stackexchange.com/questions/134649
复制相似问题