Merlin
Merlin2w ago

Did anyone succeed at file upload with the fastify adapter?

Hi there, I spent literally hours trying to figure out how to get file upload with multipart/form-data working using the fastify adapter. I couldn't get it to work. The docs (https://trpc.io/docs/server/non-json-content-types) specifically tell not to handle multipart beforehand, which is why I didn't register @fastify/multipart before registering the tRPC plugin. This however leads to Fastify rejecting multipart as non-parseable, so I tried the following:
app.addContentTypeParser(
"multipart/form-data",
(_req, body, done) => done(null, body) // hand off to tRPC
);
app.addContentTypeParser(
"multipart/form-data",
(_req, body, done) => done(null, body) // hand off to tRPC
);
After that I get a tRPC Error "400 BAD_REQUEST: Failed to parse body as FormData." Btw, if I do register @fastify/multipart, I get the same error. Also I'm using httpLink, so that's not the issue. Is there any working example for fastify? I only found one using express which is a way simpler framwork in general. Thanks in advance.
Non-JSON Content Types | tRPC
In addition to JSON-serializable data, tRPC can use FormData, File, and other Binary types as procedure inputs
5 Replies
y_nk
y_nk2w ago
you can also not use trpc for uploading files
Merlin
MerlinOP2w ago
@y_nk I kinda want to because the use of FormData as a mutation input feels incredibly clean if it works I mean if it's supposed to work and is documented it should not be something that works with express exclusively. At the very least put into the docs that it's an express-only solution that doesn't work with other adapters to spare developers hours trying to get it to work :|
y_nk
y_nk2w ago
i feel you 😄 i'm also using fastify but i gave up trying, switched to object storage signed urls instead
Merlin
MerlinOP2w ago
Thought about that too This topic feels super weird, I don't think that express is the go-to server framework people use for new apps. Fastify is so much cleaner
y_nk
y_nk2w ago
yeah i do agree (sorry for the drop :#) i just dont want to deal with multipart + having to implement a split link on top. if using object storage it works better and anyhow, there's a good amount of usage of file upload where you're going to end up storing the file somewhere permanent anyway

Did you find this page helpful?