Search code examples
javascriptbrowserbrowser-nativefs

How to use browser file system API to create an IMG


On the client side, how can I use the native browser filesystem API to embed base64 image data within an IMG element?


Solution

  • function createIMGFromFS () {
      var img = document.createElement("img");
      const openFile = async () =>  {
        const [fh] = await window.showOpenFilePicker();
        var file2 = await fh.getFile();
        var name = file2.name;
        var fr = new FileReader();
        fr.readAsDataURL(file2);
        fr.onloadend = function() {
          img.src = fr.result;
        }
      }
      const file = openFile();
      return img;
    }