英文:
Encountering a formidable plugin error within Nextjs 13
问题
I am getting this error when using Formidable with NextJS 13:
Import trace for requested module:
./node_modules/formidable/src/Formidable.js
./node_modules/formidable/src/index.js
./app/api/rest/file/route.ts
error - Error: Cannot find module 'C:\Users\jacob\Documents\GitHub\pim-nextjs-ts\yakpim\.next\server\app\api\rest\file\plugins\octetstream.js'
This is the code block:
export async function POST(request: NextApiRequest, response: NextApiResponse) {
console.log("success"); // this works
const form = formidable({ multiples: true }); // this triggers the error
}
I have tried yarn update formidable as well as 'yarn remove' and 'yarn add' again. No fix on the error.
In the node_modules > formidable folder I can see the plugin is added and used but this causes an error.
Wondering if anyone else has encountered and how you managed to resolve?
Thanks!
yarn update - no fix
yarn remove & yarn install - no fix
英文:
I am getting this error when using Formidable with NextJS 13:
Critical dependency: the request of a dependency is an expression // error
Import trace for requested module:
./node_modules/formidable/src/Formidable.js
./node_modules/formidable/src/index.js
./app/api/rest/file/route.ts
error - Error: Cannot find module 'C:\Users\jacob\Documents\GitHub\pim-nextjs-ts\yakpim\.next\server\app\api\rest\file\plugins\octetstream.js'
This is the code block:
export async function POST(request: NextApiRequest, response: NextApiResponse) {
console.log("success"); // this works
const form = formidable({ multiples: true }); // this triggers the error
I have tried yarn update formidable as well as ''yarn remove'' and ''yarn add'' again. No fix on the error.
In the node_modules > formidable folder I can see the plugin is added and used but this causes an error.
Wondering if anyone else has encountered and how you managed to resolve?
Thanks!
yarn update - no fix
yarn remove & yarn install - no fix
答案1
得分: 3
以下是翻译好的内容:
"Since formidable couldn't handle it, I imported the file directly from formdata and saved it.
In the request's formData, we converted the File object to a Buffer and stored it.
Below is an example of how this is done to save an image.
export const POST = async(req: NextRequest) => {
const f:any = await req.formData();
const obj = Object.fromEntries(f);
Object.entries(obj).forEach( async([k, v]) => {
if( !!v.type ) { // If it's a file, values like image/png are passed over.
const b = await v.arrayBuffer();
const buff = Buffer.from(b);
fs.writeFileSync(`./public/${k}`, buff);
}
});
}
If you have a fixed key value without having to convert to Object.fromEntries, you can use the
const fileObject = f.get("filename");
const b = await fileObject.arrayBuffer();
const buff = Buffer.from(b);"
英文:
Since formidable couldn't handle it, I imported the file directly from formdata and saved it.
In the request's formData, we converted the File object to a Buffer and stored it.
Below is an example of how this is done to save an image.
export const POST = async(req: NextRequest) => {
const f:any = await req.formData();
const obj = Object.fromEntries(f);
Object.entries(obj).forEach( async([k, v]) => {
if( !!v.type ) { // If it's a file, values like image/png are passed over.
const b = await v.arrayBuffer();
const buff = Buffer.from(b);
fs.writeFileSync(`./public/${k}`, buff);
}
});
}
If you have a fixed key value without having to convert to Object.fromEntries, you can use the
const fileObject = f.get("filename");
const b = await fileObject.arrayBuffer();
const buff = Buffer.from(b);
答案2
得分: 0
我为您解决了,但我没有使用formidable脚本,而是首先将文件下载为base64,然后使用node-base64-image库创建了一张图片,它有效。
import { Buffer } from 'buffer';
import { decode } from 'node-base64-image';
function makeid(length = 10) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
export async function POST(req) {
try {
const formData = await req.formData();
const file = formData.get("file");
let namefile = makeid(10)
// 将文件数据读取为Buffer
const fileData = await file.arrayBuffer();
const buffer = Buffer.from(fileData);
// 将缓冲区转换为Base64
const base64Data = buffer.toString('base64');
await decode(base64Data, { fname: "./public/upload/" + namefile, ext: "png" });
return new Response(JSON.stringify({ success: '文件成功转换为Base64' }));
} catch (err) {
console.log(err);
return new Response(JSON.stringify({ error: "服务器端错误!" }), {
status: 500
})
}
}
英文:
I solved it for you، but I didn't use formidable scripts، but I first downloaded the file to base64، then created an image using the node-base64-image library، which works.
import { Buffer } from 'buffer';
import { decode } from 'node-base64-image';
function makeid(length = 10) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
export async function POST(req) {
try {
const formData = await req.formData();
const file = formData.get("file");
let namefile = makeid(10)
// Read the file data as a Buffer
const fileData = await file.arrayBuffer();
const buffer = Buffer.from(fileData);
// Convert the buffer to Base64
const base64Data = buffer.toString('base64');
await decode(base64Data, { fname: "./public/upload/" + namefile, ext: "png" });
return new Response(JSON.stringify({ success: 'File converted to Base64 successfully' }));
} catch (err) {
console.log(err);
return new Response(JSON.stringify({ error: "Server Side Error !" }), {
status: 500
})
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论