Save a File Image to localstorage HTML

前端 未结 3 2012
傲寒
傲寒 2021-01-03 10:29

I am trying to save a image to localstorage and fetch the same back when is required,am confused on how to save a image as i have referred question related to my same questi

3条回答
  •  轮回少年
    2021-01-03 11:12

    You just lack a FileReader to read the input file to dataURL. Jsfiddle

    Html:

    
    
    
    

    javascript:

    // Get all variables
    var bannerImage = document.getElementById('bannerImg');
    var result = document.getElementById('res');
    var img = document.getElementById('tableBanner');
    
    // Add a change listener to the file input to inspect the uploaded file.
    bannerImage.addEventListener('change', function() {
        var file = this.files[0];
        // Basic type checking.
        if (file.type.indexOf('image') < 0) {
            res.innerHTML = 'invalid type';
            return;
        }
    
        // Create a file reader
        var fReader = new FileReader();
    
        // Add complete behavior
        fReader.onload = function() {
            // Show the uploaded image to banner.
            img.src = fReader.result;
    
            // Save it when data complete.
            // Use your function will ensure the format is png.
            localStorage.setItem("imgData", getBase64Image(img));
            // You can just use as its already a string.
            // localStorage.setItem("imgData", fReader.result);
        };
    
        // Read the file to DataURL format.
        fReader.readAsDataURL(file);
    });
    
    function getBase64Image(img) {
        var canvas = document.createElement("canvas");
        canvas.width = img.width;
        canvas.height = img.height;
    
        var ctx = canvas.getContext("2d");
        ctx.drawImage(img, 0, 0);
    
        var dataURL = canvas.toDataURL("image/png");
    
        return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
    }
    
    function fetchimage () {
        var dataImage = localStorage.getItem('imgData');
        img.src = "data:image/png;base64," + dataImage;
        // If you don't process the url with getBase64Image, you can just use
        // img.src = dataImage;
    }
    
    // Call fetch to get image from localStorage.
    // So each time you reload the page, the image in localstorage will be 
    // put on tableBanner
    fetchimage();
    

    Not that the jsfiddle execute this script onload, so you may wrap them in window.onload in your own site.

提交回复
热议问题