axios
.get(RequestURL, { responseType: 'blob', withCredentials: false })
.then((response) => {
let imageNode = document.getElementById('image')
let imgUrl = window.URL.createObjectURL(response.data)
console.log(imgUrl)
setImageServer(imgUrl)
})
.catch(function (error) {
// handle error
console.log(error)
})
Html
<img id='image' src={ImageServer} alt='Girl in a jacket' /> //getting an actual image(its working)
above all code is working..but the problem is converting that blob into base64
Goal:- I am trying to convert this blob to base64 so I can save it in local storage
What I have tried:-
var image = document.getElementById('image')
console.log(image.src) // blob:http://localhost:3000/1c012729-b104-4fb5-a9a5-39aa782860b4
var reader = new FileReader()
reader.readAsDataURL(image.src)
reader.onloadend = function () {
var base64data = reader.result
console.log(base64data)
}
By using this I am getting the error
TypeError: Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'.
Yes, this is in react js, But It can easily be understood as normal js(only state is used)