-1

I am building a web application that will allow users to take a photograph with the builtin camera and upload the photo to the application, which will store the photo in memory. Currently, I am using the line of code below to capture and upload the photo.

<input type="file" accept="image/*">

How do I store the image in localStorage? Is there any way to implement this feature in Javascript within the HTML?

Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • This might not be the full answer to your question, but I think [this](https://hacks.mozilla.org/2012/02/saving-images-and-files-in-localstorage/) might prove a helpful resource. – Angelos Chalaris Sep 26 '17 at 20:54

1 Answers1

1

Try something like this:

const input = document.getElementsByTagName('input')[0]; // or id or whatever selector you want to use

input.onchange = (function(e) {
  const file = e.path[0].files[0];
  const reader = new FileReader();

  reader.readAsDataURL(file);

  reader.onload = function() {
    const id = 'blobid' + (new Date()).getTime();
    const blobCache = tinymce.activeEditor.editorUpload.blobCache;
    const base64 = reader.result.split(',')[1];
    localStorage.setItem('imageString', base64);
  };
});
CascadiaJS
  • 2,320
  • 2
  • 26
  • 46