I'm trying to use react-avatar-edit to crop profile image before uploading it, I made implementation successfully, and now I can get cropped image but here is the problem, the cropped image is retrieved in base64string first issue was trying to convert it to an image and after research I found the code below to convert it to blob after some tries to solve it in several ways
// code from stack overflow link: https://stackoverflow.com/questions/16245767/creating-a-blob-from-a-base64-string-in-javascript#answer-36183085
convertBase64ToBlob = (url) =>{
fetch(url)
.then(res => res.blob())
.then(blob => {
this.convertBlobToFormData(blob)
})
;
}
then I want to send it as a file so i used this
convertBlobToFormData = (blob) => {
let data = new FormData();
data.append('profile_image', blob)
this.setState({
convertedFile: data
})
}
and calling the functions happens when the user drag&drop the crop shape you can see this link for more details about how the package work
onCrop(preview) {
this.setState({preview})
this.convertBase64ToBlob(preview)
console.log(this.state.convertedFile)
if(this.state.convertedFile){
this.props.onChange(this.state.convertedFile)
};
}
the issue is while printing blob in console.log it retrieves
BlobĀ {size: 1805, type: "text/html"}
and that means it failed to convert base46string to image that I can send it to the endpoint so what's wrong in this or how I can fix it.
the second issue while printing form data that we retrieve from convertBlobToFormData it retrieves enter image description here
so why storing the blob retrieves an empty form data and how I can solve it
note: the backend I use is laravel backend developed by another developer