Skip to content

Upload File #164

Closed Answered by TheEdoRan
Kavehrafie asked this question in Q&A
Jun 13, 2024 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

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,
    };
  });

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Kavehrafie
Comment options

Answer selected by TheEdoRan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants