When attempting to retrieve form data that potentially includes multiple file types intended for upload to Firebase Storage, encountering an error message indicating “invalid file type”.
please look into this issue and let me know where i am wrong
here is the code:
const uploadFile = async (file, filePath) => {
console.log('File:', file);
console.log('File type:', typeof file);
console.log('File name:', file.name);
const storageRef = ref(storage, filePath);
try {
if (typeof file === 'string' && file.startsWith('data:')) {
// If file is a Data URL, extract base64 data and upload
it using uploadBytes
const contentType = file.split(';')[0].split(':')[1];
const base64Data = file.split(',')[1];
const byteArray = Uint8Array.from(atob(base64Data), c =>
c.charCodeAt(0));
const blob = new Blob([byteArray], { type: contentType });
await uploadBytes(storageRef, blob);
} else if (file instanceof Blob) {
// If file is a Blob, upload it directly
await uploadBytes(storageRef, file);
} else if (file instanceof File) {
// If file is a File object, upload it directly
await uploadBytes(storageRef, file);
} else {
throw new Error('Invalid file type');
}
// Return the download URL
return getDownloadURL(storageRef);
} catch (error) {
console.error('Error uploading file:', error);
throw error; // Rethrow the error to be caught in the
handleSubmit function
}
};
New contributor
Navanit Krish K M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.