Tried to upload image (jpeg,png) to nft storage IPFS, but unable to upload as the code throws the error as:
Error uploading file to IPFS: Error at pRetry__default.default.retries (C:UsersDELLDownloadsEMPVERIFYnode_modulesnft.storagedistsrclib.cjs:242:23) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async RetryOperation._fn (C:UsersDELLDownloadsEMPVERIFYnode_modulesp-retryindex.js:50:12)
upload.js
const router = express.Router()
const upload = multer({ dest: 'uploads/' });
const { NFTStorage, File } = require('nft.storage');
const fs = require('fs').promises;
const path = require('path');
const client = new NFTStorage({ token: process.env.NFT_STORAGE_API_TOKEN })
router.post('/uploadToIPFS', upload.single('studentDocument'), async (req, res) => {
const { originalname: fileName, path: tempFilePath, mimetype } = req.file;
try {
const fileData = await fs.readFile(tempFilePath);
const metadata = await client.store({
name: req.body.name || 'File',
description: req.body.description || 'description',
image: new File([fileData], fileName, { type: mimetype })
});
const cid = metadata.ipnft.toString();
const metadata = await client.storeBlob(blob)
console.log(metadata)
catch (error) {
console.error('Error uploading file to IPFS:', error);
res.status(500).json({ error: 'Failed to upload file to IPFS' });
} finally {
await fs.unlink(tempFilePath);
}
})
index.ejs
<html>
<body>
<form id="documentUploadForm" enctype="multipart/form-data" class="d-flex justify-content-between">
<input type="file" name="studentDocument" class="form-control w-50 ps-2 " style="height: 35px;">
<button type="submit" id="docUpload" class="btn btn-lg fw-bolder form-btn py-1 px-3 float-end">click to Add
</button>
<script>
document.getElementById('docUpload').addEventListener('click', async (event) => {
event.preventDefault();
const form = document.getElementById('documentUploadForm');
const formData = new FormData(form);
try {
const response = await fetch('/uploadToIPFS', {
method: 'POST',
body: formData ,
});
if (response.ok) {
const data = await response.json();
console.log(data)
}
})
</script>
</body>
</html>
Tried to upload image (jpeg,png) to nft storage IPFS but the above code throws an error as unable to upload file but I followed the docs
New contributor
Sadanand Miskin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.