Upload File #164
Upload File
#164
-
This is great package but I could not figure out how I can get uploaded file given that there is no access to formData inside the action callback |
Beta Was this translation helpful? Give feedback.
Answered by
TheEdoRan
Jun 13, 2024
Replies: 1 comment 1 reply
-
I added a file upload recipe on the docs website. This seems to work fine: "use server";
import { action } from "@/lib/safe-action";
import { writeFileSync } from "fs";
import { zfd } from "zod-form-data";
const schema = zfd.formData({
image: zfd.file(),
});
export const fileUploadAction = action
.schema(schema)
.action(async ({ parsedInput }) => {
await new Promise((res) => setTimeout(res, 1000));
// Write file to disk
writeFileSync(
parsedInput.image.name,
Buffer.from(await parsedInput.image.arrayBuffer())
);
// Do something useful with the file.
console.log("fileUploadAction ->", parsedInput);
return {
ok: true,
};
}); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
TheEdoRan
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I added a file upload recipe on the docs website.
This seems to work fine: