Robin Khan
Robin Khan

Reputation: 223

Argument of type Buffer is not assignable in writeFile

I am trying to upload image in local file system using next.js 14 in a server action file. But writeFile function from "fs/promises" is not taking Buffer as second argument. Here is my code snippet for that function

'use server'

import path from "path";
import { writeFile } from "fs/promises";


async function uploadImage(file: File) {
    try {
        // upload file logic here
        const uploadedFileName = `${Date.now()}_${file.name}`;
        const filePath = path.join(process.cwd(), 'public', 'images', uploadedFileName);

    const arrayBuffer = await file.arrayBuffer();
    const buffer = Buffer.from(arrayBuffer);
    await writeFile(filePath, buffer); // This line causing the issue 
} catch (error) {
    console.log(error);
    throw error;
}

}

await writeFile(filePath, buffer); 

This is causing compile time issue in my ide (VS code) saying that argument of type 'Buffer' is not assignable to type of 'string | ArrayBufferView | ...

How can I get this to work?

Note: I was using Typescript for the whole project and this is also a typescript file

Upvotes: 0

Views: 134

Answers (0)

Related Questions