【问题标题】:What is the format of the URL from google image search results? And how to save the image with javascript?谷歌图片搜索结果中的 URL 格式是什么?以及如何用javascript保存图像?
【发布时间】:2021-11-21 13:27:20
【问题描述】:

当我在谷歌上搜索图片并从右键菜单中选择copy image address 时,地址不是传统的https://...。事实上,它是一个以data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD... 开头的大字符串。这是什么?另外如何使用此图像将其保存到文件中?

【问题讨论】:

    标签: javascript image url fetch


    【解决方案1】:

    网址是Data URL。您可以按照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);
    

    【讨论】:

      猜你喜欢
      • 2013-10-24
      • 1970-01-01
      • 2016-06-18
      • 2016-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      相关资源
      最近更新 更多