Untitled
const docUploadValidation = (fileImage) => { const maxFileSize = 4 * 1024 * 1024; // 4MB const validImageTypes = ['image/jpeg', 'image/png', 'image/jpg']; if (fileImage?.length > 0) { let file = fileImage[0].file; // Check if the file type is 'Blob' and convert it if (file instanceof Blob) { // Get the file extension from the filename and set the appropriate MIME type const fileExtension = fileImage[0].fileExtension.toLowerCase(); let mimeType = ''; if (fileExtension === 'png') { mimeType = 'image/png'; } else if (fileExtension === 'jpg' || fileExtension === 'jpeg') { mimeType = 'image/jpeg'; } else { mimeType = 'image/jpg'; // Default image type } // Convert the Blob to a File with the desired type and name const fileName = fileImage[0].filenameWithoutExtension + '.' + fileExtension; file = new File([file], fileName, { type: mimeType }); } if (file.size > maxFileSize) { toast.error("Image size should not exceed 4MB", { autoClose: 3000 }); return false; } if (!validImageTypes.includes(file.type)) { toast.error("File type should be an image (JPEG, PNG, or JPG)", { autoClose: 3000 }); return false; } } return true; };
Leave a Comment