0

When I search images on google and select copy image address from the right-click menu, the address isn't a conventional https://... one. In fact it's a massive string that begins with data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD.... What is this? Also how do I use this image to save it to a file?

JimmyTheCode
  • 3,783
  • 7
  • 29
  • 71

1 Answers1

0

The URL is a Data URL. You can save it to a file as per this explanation:

 function dataURLtoFile(dataurl, filename) {
 
        var arr = dataurl.split(','),
            mime = arr[0].match(/:(.*?);/)[1],
            bstr = atob(arr[1]), 
            n = bstr.length, 
            u8arr = new Uint8Array(n);
            
        while(n--){
            u8arr[n] = bstr.charCodeAt(n);
        }
        
        return new File([u8arr], filename, {type:mime});
    }
    
    //Usage example:
    var file = dataURLtoFile('data:text/plain;base64,aGVsbG8gd29ybGQ=','hello.txt');
    console.log(file);
JimmyTheCode
  • 3,783
  • 7
  • 29
  • 71