2
         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)

sanket kheni
  • 1,482
  • 2
  • 12
  • 29
  • 1
    I think this one can answer your question pretty well: [How can I convert an image into Base64 string using JavaScript?](https://stackoverflow.com/questions/6150289/how-can-i-convert-an-image-into-base64-string-using-javascript) – Alexis May 05 '21 at 13:03

2 Answers2

1

Not knowing what the function setImageServer() does, it's probably because URL.createObjectURL() returns a DOMString (which is basically a string type):

https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL

https://developer.mozilla.org/en-US/docs/Web/API/DOMString

So your console.log is printing out a string representation of your blob object, not the blob itself. You could maybe try this to convert your DOMString to base64:

https://reference.codeproject.com/book/javascript/base64_encoding_and_decoding

PhunkmasterP
  • 136
  • 1
  • 8
0

The problem was I am using image.src rather than using direct response from the server like respone.data

this is the solution.

  axios
                .get(RequestURL, { responseType: 'blob', withCredentials: false })
                .then((response) => {
                    console.log(response.data)
                    let imgData = window.URL.createObjectURL(response.data)
                    setImageServer(imgData)
                    var reader = new FileReader()
    
                    reader.readAsDataURL(response.data)
                    reader.onloadend = function () {
                        var base64data = reader.result
                        console.log(base64data)
                    }
                })
sanket kheni
  • 1,482
  • 2
  • 12
  • 29