Is it possible to return an arbitrary Response from a form action? #9948
-
In an API GET I could write something to return an arbitrary Response, like: // API endpoint to get some file and initiate user download
export const GET = async ({ cookies }) => {
// ...
const someFile = fs.readFileSync(`someFilePathOnDrive`);
return new Response(someFile, {
status: 200,
headers: {
'Content-type': 'multipart/form-data',
'Content-Disposition': `attachment; filename=someFileName.someFileExtension`
}
});
}; And it will download that file for the user. This file is not serializable. Is it not possible in SvelteKit to do the same thing with form actions? Perhaps I'm simply overlooking something, or not understanding a limitation. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
Sounds like it could work if you throw a redirect to the Or would it work by simply using the |
Beta Was this translation helpful? Give feedback.
-
@simonsarris Did you find a solution for your problem? I am trying to return a csv from a form action call. |
Beta Was this translation helpful? Give feedback.
-
No it is not possible, actions are designed to populate the You will have to implement your form handling the "normal" way using an API endpoint as described here: https://kit.svelte.dev/docs/form-actions#alternatives |
Beta Was this translation helpful? Give feedback.
-
Yeah, I just ended up using an API endpoint. |
Beta Was this translation helpful? Give feedback.
No it is not possible, actions are designed to populate the
form
property of a page and therefore have to be fully serializable. A generic file, image or csv doesn't fall into that category. So the result from an action always has to be a simple object.You will have to implement your form handling the "normal" way using an API endpoint as described here: https://kit.svelte.dev/docs/form-actions#alternatives