简体   繁体   中英

saving image to localstorage from form and storing/loading it

my code looks like this;

<form>
<input type="text" />
<input type="file">
</form>

<div id="notes"></div>

i got the text variables to work, however, i cannot get this silly image thing to work, i've looked at loads of tutorials but i simply cannot manage to do it

i know i have to do something with

(document.getElementById("file").files)[0] != null) {
var pic = (document.getElementById("file").files)[0];
var imgUrl;
var reader = new FileReader();  
reader.onload = function(e) {
var imgURL = reader.result;
saveDataToLocalStorage(imgURL);
reader.readAsDataURL(pic);

for the image and then use the JSON.parse to get the url back and show it on the page

but i cannot figure out how it works, neither can i find any examples that aren't too complicated to implement it into my own code

in this fiddle i have provided all the code that i have at the moment http://jsfiddle.net/VXdkC/

i really hope you guys can help me out, i've been messing around with this thing the past 2 days and it's starting to frustrate me :(

Here's how I'd do it :

var notes = localStorage.getItem('notes'),
    arr   = [];

if (notes) {
    arr = JSON.parse(notes);

    $.each(arr, function(k,v) {
        console.log(v)
        var h1  = $('<h1 />', {text: v.title});
        var p   = $('<p />', {text: v.msg});
        var img = $('<img />', {src: v.image});

        $('#notes').append(h1, p, img);
    });
}

$('#clear').click(function () {
    if (confirm('This will clear all notes, are you sure?')) {
        window.localStorage.setItem('notes','');
        location.reload();
    }
    return false;
});

$('#addNote').click(function () {
    var Title = $('#title').val();
    var Message = $('#message').val();
    var file = $('#file').prop('files')[0];

    var reader = new FileReader();
    reader.readAsDataURL(file);

    reader.onload = function (e) {
        var b64 = e.target.result;
        var note = {
            image : b64,
            title : Title,
            msg   : Message
        }
        arr.push(note);
        localStorage.setItem('notes', JSON.stringify( arr ));
        $('#notes').prepend("<div class='entry'><h1>" + Title + "</h1></p>" + "<p>" + Message + "<img src=" + b64 + " /></p> </div>");
    }
    return false;
});

FIDDLE

Its pretty simple

var pic = document.getElementById("file").files[0];
var imgUrl;
var reader = new FileReader();  
reader.onload = function(e) {
  var imgURL = reader.result;
  $('#notes').prepend("<div class='entry'><h1>" + Title + "</h1></p>" + "<p>" + Message + "<img src=" + imgURL + "></p> </div>");

  var notes = $('#notes').html();
  localStorage.setItem('notes', notes);
  saveDataToLocalStorage(imgURL);
}
reader.readAsDataURL(pic);

http://jsfiddle.net/VXdkC/2/

Live demo here (click).

the html:

<input id="file" type="file">

the js:

var fileInput = document.getElementById('file');

fileInput.addEventListener('change', function(e) {
  var reader = new FileReader(); //create reader
  reader.onload = function() { //attach onload
    //do something with the result
    console.log(reader.result);
    localStorage.img = reader.result; //saved to localStorage
    createImg(localStorage.img); //retrieved from localStorage
  };
  var file = e.target.files[0];
  reader.readAsDataURL(file); //trigger onload function
});

function createImg(dataUri) {
  var img = document.createElement('img');
  img.src = dataUri;
  document.body.appendChild(img);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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