Skip to content
This repository was archived by the owner on Oct 8, 2025. It is now read-only.
This repository was archived by the owner on Oct 8, 2025. It is now read-only.

[form-data-parser] maxFileSize crash my route/server/fetcher #60

@guelosuperstart

Description

@guelosuperstart

Hello,

I tried adding maxFileSize, but when the uploaded file exceeds the defined limit, the fetcher stops, and I’m unable to capture any error response.

I’ve implemented a catch block with if (error instanceof MaxFileSizeExceededError), which seems to be correct, but I’m not getting any return or feedback from the fetcher when this happens.

When the file size is below the maxFileSize, everything works perfectly.

Do you have any advice on how I can properly catch and handle the error when the file is too large?

Thanks!

const ImageButton = ({ editor }: { editor: Editor | null }) => {
  const fetcher = useFetcher();
  const inputRef = useRef<HTMLInputElement>(null);

  useEffect(() => {
    if ('idle' === fetcher.state && fetcher.data) {
      console.log(fetcher.data);
    }
  }, [fetcher.state, fetcher.data, editor]);

  if (!editor) {
    return null;
  }

  const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    const files = event.target.files;
    if (files && files.length > 0) {
      const formData = new FormData();
      formData.append(emailImageFieldName, files[0]);
      fetcher
        .submit(formData, { method: 'post', action: appPaths.image.upload, encType: 'multipart/form-data' })
        .finally(() => event.target.value = '');
    }
  };

  return (
    <>
      {JSON.stringify(fetcher)}
      <span className="sr-only">Insert image</span>
      <ToolbarButton onClick={() => inputRef.current?.click()} isActive={editor.isActive('image')} icon={PhotoIcon} label="Insert image" />
      <input type="file" name={emailImageFieldName} accept="image/*" ref={inputRef} onChange={handleFileChange} />
    </>
  );
};
import type { Route } from './+types/upload.image._index.route';
import { type FileUpload, parseFormData } from '@mjackson/form-data-parser';
import { createStorageKey, fileStorage } from '~/routes/upload/email-storage.server';
import { appPaths } from '~/core/appPaths';
import { emailImageFieldName } from './upload.type';
import { MaxFileSizeExceededError } from '@mjackson/multipart-parser';

let storageKey: string;

async function uploadHandler(fileUpload: FileUpload) {
  if (emailImageFieldName === fileUpload.fieldName && fileUpload.type.startsWith('image/')) {
    storageKey = createStorageKey();

    await fileStorage.set(storageKey, fileUpload);

    return await fileStorage.get(storageKey);
  }
}

export async function action({ request }: Route.ActionArgs) {
  try {
    const formData = await parseFormData(request, { maxFileSize: 1000000 }, uploadHandler);
    console.log(formData);
    const file = formData.get(emailImageFieldName);

    if (file instanceof File) {
      return { name: file.name, src: `${appPaths.image.email(storageKey)}` };
    }

  } catch (error: any) {
    if (error instanceof MaxFileSizeExceededError) {
      return { success: false, message: error.message };
    }
  }

  return null;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions