简体   繁体   English

无需上传即可将Image转换为base64

[英]convert Image to base64 without uploading

i want to save image in localStorage in base 64 format.I know it using Filereader concept like this 我想以base 64格式将图像保存在localStorage中。我使用Filereader这样的概念知道它

function loadImageFileAsURL()
{
    var filesSelected = document.getElementById("inputFileToLoad").files;

    if (filesSelected.length > 0)
    {
        var fileToLoad = filesSelected[0];


        var fileReader = new FileReader();

        fileReader.onload = function(fileLoadedEvent) 
        {
        alert(fileLoadedEvent);

            document.getElementById("textAreaFileContents").innerHTML = fileLoadedEvent.target.result;
        };

        fileReader.readAsDataURL(fileToLoad);
    }
}

but the problem is that i dont want upload any file. 但问题是我不想上传任何文件。 I just want to convert existing image into base64.Hope someone can help it 我只想将现有图像转换为base64。希望有人可以帮忙

Yeah it's kind off possible ;) 是的,这很可能;)

  1. Create canvas element 创建画布元素
  2. Load your image onto it 将图像加载到其上
  3. Run canvas.toDataURL() (base64 encoded image) 运行canvas.toDataURL() (以base64编码的图像)
  4. Save it locally or anywhere you want 将其保存在本地或您想要的任何位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM