I need to convert media into File type to upload with JS.
The below code gives me the correct File output when taking a picture from the BUILT IN camera on my computer:
const convertToFile = (base64: string) => {
let arr = base64.split(","),
mime = arr[0].match(/:(.*?);/)![1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
const extension = mime.split("/")[1];
console.log(extension);
const fileName = new Date().getTime() + `.${extension}`;
return new File([u8arr], fileName, { type: mime });
};
BUT when I try to convert a picture taken from my MOBILE PHONE CAMERA (runing it through the exact same code), this is the output that I get which is "corrupt".
**Please note I must convert this image to File, because of how the backend is structure I need to send it as a File (not as a Blob).