Media upload via Next.js API routes without cloud services

22 views Asked by At

I am developing an application in Next.js hosted on vercel. It also has a dashboard which is a standalone application also written in Next.js. I am restricted to upload images without any third-party upload services such as cloudinary. What I have done is hitting the POST API of my main app from the dashboard and sending image as Base 64. Here is the code for that API:

import { writeFile } from "fs/promises"
import { NextResponse } from "next/server"

export async function POST(req){
    const data=await req.formData();
    const file=data.get('file');
    if(!file){
        return NextResponse.json({"message":"no image found",success: false});
    }
    const byteData=await file.arrayBuffer();
    const buffer=Buffer.from(byteData);
    const path=`./${file.name}`;
    await writeFile(path,buffer);
    return NextResponse.json({"message":"file uploaded",success:true});
}

But when I hit this API, I get vercel error that I cannot write on a read-only system. Is it just a vercel thing or is there no way to upload media without Cloud services? Any solution or suggestion is appreciated.

0

There are 0 answers