I have being trying to upload image from frontend to backend but cannot workaround my head over this.
Frontend
Send some data including “image” to backend.
Backend
Below is my backend code
<code>//Imports
import { NextResponse } from 'next/server';
import fs from 'fs';
import formidable from 'formidable';
//disabling nextjs default body parser
export const config = {
api: { bodyParser: false },
};
export async function POST(request) {
//get data from body
const formData = await request.formData();
//get logo image file
const logo = formData.get('business_logo');
console.log(logo);//output is below
//default
return NextResponse.json(request)
};
</code>
<code>//Imports
import { NextResponse } from 'next/server';
import fs from 'fs';
import formidable from 'formidable';
//disabling nextjs default body parser
export const config = {
api: { bodyParser: false },
};
export async function POST(request) {
//get data from body
const formData = await request.formData();
//get logo image file
const logo = formData.get('business_logo');
console.log(logo);//output is below
//default
return NextResponse.json(request)
};
</code>
//Imports
import { NextResponse } from 'next/server';
import fs from 'fs';
import formidable from 'formidable';
//disabling nextjs default body parser
export const config = {
api: { bodyParser: false },
};
export async function POST(request) {
//get data from body
const formData = await request.formData();
//get logo image file
const logo = formData.get('business_logo');
console.log(logo);//output is below
//default
return NextResponse.json(request)
};
Output
<code>File {
size: 686117,
type: 'image/jpeg',
name: '102311052-customer moldbase - ZOOM in to obscure customer content.JPEG',
lastModified: 1721648713221
}
</code>
<code>File {
size: 686117,
type: 'image/jpeg',
name: '102311052-customer moldbase - ZOOM in to obscure customer content.JPEG',
lastModified: 1721648713221
}
</code>
File {
size: 686117,
type: 'image/jpeg',
name: '102311052-customer moldbase - ZOOM in to obscure customer content.JPEG',
lastModified: 1721648713221
}
Issue:
How to get file path or buffer (which is needed to process file further like uploading to bunnycdn or save to local storage). I also tried “fs” to readFile but not luck as the above output is object.