Skip to content

FormData input type="file" returns an empty object in serverFn #3983

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

Open
KirdesMF opened this issue Apr 12, 2025 · 2 comments
Open

FormData input type="file" returns an empty object in serverFn #3983

KirdesMF opened this issue Apr 12, 2025 · 2 comments

Comments

@KirdesMF
Copy link

Which project does this relate to?

Start

Describe the bug

Hello, trying to validate input file on the server but the validation fails because it returns an empty object

issues: [
    {
      kind: 'schema',
      type: 'instance',
      input: {},
      expected: 'File',
      received: 'Object',
      message: 'Veuillez entrer un fichier valide',
      requirement: undefined,
      path: [Array],
      issues: undefined,
      lang: undefined,
      abortEarly: undefined,
      abortPipeEarly: undefined
    }
  ]
export const AddCompanySchema = v.object({
    logo: v.pipe(
        v.instance(File, "Veuillez entrer un fichier valide"),
        v.mimeType(["image/png", "image/jpeg", "image/jpg"], "Veuillez entrer un fichier valide"),
        v.maxSize(1024 * 1024 * 3, "La taille du fichier doit être inférieure à 3MB"),
    ),
});

Your Example Website or App

https://stackblitz.com/edit/tanstack-router-cegcegst?file=src/routes/index.tsx&preset=node

Steps to Reproduce the Bug or Issue

  1. create a tanstack/start app
  2. create form with a input file
  3. create a server function
  4. log the form data
  5. it logs an empty object

Expected behavior

we should have a File object

Screenshots or Videos

No response

Platform

  • OS: macOS
  • Browser: Arc
  • Version: 1.90.1

Additional context

No response

@Callme-VR
Copy link

i Think ,
createServerFn handler is trying to access context that isn't available because it's not running inside the expected server environment provided by @tanstack/react-start,but this is run on Server Side

so i try resolve this issue like we cannot call createServerfn directly in client like regular function,so we do use Fetch-style api that goes through server

i new so keep kindly to my solution,pls if anyone like this, comment and follow ,give me star

Tenstack_issue_sol.txt

@KirdesMF
Copy link
Author

KirdesMF commented Apr 12, 2025

Yes I'm not sure a bout the context issue, the error was there when I forked the stackblitz.
But I've found a solution to my issue. Instead of getting data client side, I just send the plein form data and get item server side.

No context issue here in a codesandbox:
https://codesandbox.io/p/devbox/github/tanstack/router/tree/main/examples/react/start-basic?embed=1&theme=dark

ServerFn Request: src_routes_index_tsx--getFileFormData_createServerFn_handler
from validator File {
  size: 18843,
  type: 'image/jpeg',
  name: 'avatar.JPG',
  lastModified: 1744495844363
}
form data server {
  img: File {
    size: 18843,
    type: 'image/jpeg',
    name: 'avatar.JPG',
    lastModified: 1744495844363
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants