Cannot save images to NFT.Storage using API call in NEXT.js

Viewed 13

Hi I am trying to use Next.js api routes to store an image file on NFT.storage. I am taking an image as an input from the user in the browser and passing that to the api file using Axios and FormData const formData = new FormData(); formData.append('image', imageUrl); const cid = await axios.post('api/uploadNFTData', formData);

here the imageUrl is event.target.files[0] and in the api folder I have uploadNFTData.js containing this code I have done this to keep my API key more secure by taking the value from the .env

import { NFTStorage, File, Blob } from 'nft.storage';
import formidable from 'formidable';
const client = new NFTStorage({ token: process.env.NFT_STORE_TOKEN});
// first we need to disable the default body parser
export const config = {
 api: {
   bodyParser: false,
  },
}
export default async function uploadNFTData(req,res) {
const form = new formidable.IncomingForm();
form.uploadDir = "./";
form.keepExtensions = true;
var formfields = await new Promise(function (resolve, reject) {
form.parse(req, (err, fields, files) => {
if (err) {
  reject(err);
  return;
 }
resolve(files);
console.log("within form.parse method, subject field of fields object is: " + files);
  }); // form.parse
});
try{
   const cid = await client.storeDirectory([formfields]);
   res.status(200).json({ message: cid });
  }catch(err){
 res.status(500).json({ message: "could not upload data" });
 }
}

Now I am getting the following error on server side error - TypeError: blob.stream is not a function Can someone please help regarding what I am doing wrong?? Or This kind of call cannot be done??

0 Answers
Related