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;
}
Hello,
I tried adding
maxFileSize, but when the uploaded file exceeds the defined limit, thefetcherstops, and I’m unable to capture any error response.I’ve implemented a
catchblock withif (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!