How can I get file from form data in api
Unanswered
Iridescent shark posted this in #help-forum
Iridescent sharkOP
import { Ok, withErrorHandler } from "@/lib/api";
import { BadRequestError } from "@/lib/errors";
import googleStorageService from "@/lib/services/googleStorageService";
import { NextRequest } from "next/server";
export const POST = withErrorHandler(async (req: NextRequest) => {
const formData = await req.formData();
const file = formData.get("file") as File | null;
if (!file) {
throw new BadRequestError("No file provided");
}
if (file.size > 1024 * 1024 * 5) {
throw new BadRequestError("File is too large");
}
const buffer = await file.arrayBuffer();
const fileName = file.name;
const url = await googleStorageService.upload(fileName, buffer, "tmp");
const uploadedFile = {
name: file.name,
size: file.size,
type: file.type,
url,
};
return Ok(uploadedFile);
});
I get error on line const formData = await req.formData(); . I tried to turn off bodyParser in config but if I build I get error that config is deprecated.
import { BadRequestError } from "@/lib/errors";
import googleStorageService from "@/lib/services/googleStorageService";
import { NextRequest } from "next/server";
export const POST = withErrorHandler(async (req: NextRequest) => {
const formData = await req.formData();
const file = formData.get("file") as File | null;
if (!file) {
throw new BadRequestError("No file provided");
}
if (file.size > 1024 * 1024 * 5) {
throw new BadRequestError("File is too large");
}
const buffer = await file.arrayBuffer();
const fileName = file.name;
const url = await googleStorageService.upload(fileName, buffer, "tmp");
const uploadedFile = {
name: file.name,
size: file.size,
type: file.type,
url,
};
return Ok(uploadedFile);
});
I get error on line const formData = await req.formData(); . I tried to turn off bodyParser in config but if I build I get error that config is deprecated.