-
-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Zero bytes file saved with attachFieldsToBody options #200
Comments
Can you please include a runnable example and script to reproduce this behavior? It's hard to help/diagnose these problems otherwise. |
working on it |
upload method:
Same problem with attachFieldsToBody |
Could this be related to the default 1MB body limit? |
I have just tried to change limit and it still doesn't work |
console.log of the body |
Here's a repro of the issue. Reading the stream won't work, using The reason can be found in these lines of code. The file stream is read with I believe this is done because the request body needs to be processed in full before proceeding. I also verified that indeed the size limits are not validated eagerly but only after the request body is parsed. Now this all looks like a bug but I guess it's somewhat intended. As you can see in the linked code an alternative to using const fs = require("fs");
const pump = require("pump");
const server = require("fastify")();
server.register(require("."), {
attachFieldsToBody: true,
});
const handler = async (req) => {
const part = await req.body.file;
const filePath = `uploads/${part.filename}`;
// does not work
await pump(part.file, fs.createWriteStream(filePath));
// works
// fs.writeFileSync(filePath, await part.toBuffer());
return "ok";
};
server.post("/upload", handler);
server.listen(3000); |
I faced the same issue and proved that @simoneb was right. As the documentation states, if you assign all fields to the body and don't define an The only way to access the file content is through the I think the documentation might be more explicit about this. |
Awesome, thanks! |
🐛 Bug Report
My file saved was zero bites when i use the

attachFieldsToBody: true
options.in this case file limit the limit file is also not working properly. It takes 2 minutes for a file size of 90MB to display the message and a message
request file too large, please check multipart config
This is my route handler.
This is the register settings
This my validation schema
my code was works fine when i use the normal register options only
fileSize
limits. but i need to send another field to my handlerIs there something wrong?
*sorry for my bad english
Expected behavior
The file size I upload must match what I submitted from the form
Environment
The text was updated successfully, but these errors were encountered: