Next.js Discord

Discord Forum

Nextjs sends file with undefined name

Unanswered
Chinese softshell turtle posted this in #help-forum
Open in Discord
Chinese softshell turtleOP
I have a form with plain <input type="file"/>, with a handler
onChange={(e) => {
            const file = e.target.files?.item(0) ?? null;
            setFileSelected(file);
          }}

And form handler being a server action: action={action}. However, when file is not selected (it is reflected by file state being set to null), in my Server action, after getting this file from FormData, I get this placeholder, instead of null:
File {
  size: 0,
  type: 'application/octet-stream',
  name: 'undefined',
  lastModified: 1692780165744
}

Which is strange, because I expected it to be null. I know I can just check for size === 0, but I'm curious if it is some documented behaviour or even if Nextjs is responsible for this (maybe it's a browser thing).

0 Replies