I want to be able to upload multiple images to Firebase using React Typescript.
The uploadBytes() is throwing some type error which I have been unable to resolve.
Below is my Code
type imageUploadType = {
lastModified: number,
lastModifiedDate: Date,
name: string,
size: string,
type: string,
webkitRelativePath: string
}
const initialUploadValues = {
lastModified: 1688490575474,
lastModifiedDate: new Date(),
name: "Gospel TV.png",
size: '24546',
type: "image/png",
webkitRelativePath: ""
}
const Modal = (props: IModalProps) => {
const user = useContext(UserContext)
const [imageUpload, setImageUpload] = useState<imageUploadType>(initialUploadValues);
const [imageUrls, setImageUrls] = useState<Array<string>>([]);
const imagesListRef = ref(storage, "images/");
const uploadFile = () => {
if (imageUpload == null) return;
const imageRef = ref(storage, `images/${imageUpload.name + v4()}`);
uploadBytes(imageRef, imageUpload).then((snapshot) => {
getDownloadURL(snapshot.ref).then((url) => {
setImageUrls((prev) => [...prev, url]);
});
});
};
}
Below is the error
Argument of type ‘imageUploadType’ is not assignable to parameter of type ‘Blob | Uint8Array | ArrayBuffer’.
Type ‘imageUploadType’ is missing the following properties from type ‘Blob’: arrayBuffer, slice, stream, textts(2345)
const imageUpload: imageUploadType
i tried adding a custom type for the imageUpload state and yet didnt work
2
Answers
Based on the docs found here, it looks like the function
uploadBytes
is expecting a different datatype than you are passing into it. Here is a proposed solution by converting passed in data to a blob:convertToBlob
is going to take in a file object and convert it into a blob object. The returned promise resolves with the resulting blob.uploadFile
now creates a file object usingimageUpload
and passing it to theconvertToBlob
function to obtain the converted Blob before uploading it to Firebase.One thing to double check is the
convertToBlob
function uses the FileReader API to read the file content. So you can swap this out with whatever else you’re currently using to read files, or import it if you’re not reading files at all right now.What is happening is that
uploadBytes
function expects the image to be formatted asBlob
, you must convert the image file toBlob
before running your functionuploadBytes
const fileBlob = new Blob([urlImageToUpload], { type: urlImageToUpload.type });
this it’s the documentation.
https://developer.mozilla.org/es/docs/Web/API/Blob