Next.js Discord

Discord Forum

unable to upload files after deploying on vercel

Unanswered
African Slender-snouted Crocodil… posted this in #help-forum
Open in Discord
African Slender-snouted CrocodileOP
This is how I implemented it but, does not seem to work as I get an error stating that no such file or directory '/vercel'

  const __filename = fileURLToPath(import.meta.url);
  const __dirname = path.join(__filename);;

  const directoryPath = path.resolve(__dirname, 'public');
    const body = await req.formData()

    const file = body.get("file") as File;

    const buffer = Buffer.from(await file.arrayBuffer());
    const relativeUploadDir = `/uploads`

      const uploadDir = path.join(process.cwd(), relativeUploadDir);

console.log(uploadDir)
    console.log(file.type)

    try {
        await stat(uploadDir);
      } catch (e: any) {
        if (e.code === "ENOENT") {
          // This is for checking the directory is exist (ENOENT : Error No Entry)
          await mkdir(uploadDir, { recursive: true });
        } else {
          console.error(
            "Error while trying to create directory when uploading a file\n",
            e
          );
          return NextResponse.json(
            { error: "Something went wrong." },
            { status: 500 }
          );
        }
      }

2 Replies