0

I'm trying to allow a user to click a file upload button, select a file, and have that file immediately display wherever they are typing in a contenteditable div. I have a solution that works inconsistently, and seems to depend on which element is focused before the file upload is clicked.

This is similar to this question, except that I cannot get the proposed solution to work reliably:

insert image inside a contenteditable div

Here is an example that works sometimes, but every time I run it it is hard to predict whether or not the image will show. I'm trying to make this work reliably.

https://jsfiddle.net/0tdgc49k/1/

Note that I am clicking on the label, not the input element, which should trigger the same event. Uploads using the input element seem more reliable, although I would ideally like to get the label element working because it is easier to style.

function pasteImage() {
 document.getElementById("output").focus();
 var reader = new FileReader();
  reader.onload = function(e) {
   document.getElementById("output").focus();
   document.execCommand('insertImage', false, e.target.result);
  }
  reader.readAsDataURL(document.getElementById("input").files[0]);
}

document.getElementById("input").addEventListener('input', pasteImage);
document.getElementById("output").focus();
input {
  opacity: 0;
}
<label for="input">Upload Photo</label>
<input id="input" type="file" accept="image/*">
<div id="output" tabindex="0" contenteditable="true">Editable Content</div>

I expect that every time I click "Upload Image" and select an image from my harddrive, it will become visible in the contenteditable div. Sometimes it does, but if I repeatedly run the fiddle and attempt the upload again, it fails about half the time. This result is consistent across both the fiddle and the local html page on which I developed it, so it is not just an artifact of the fiddle.

  • I cannot reproduce your issue, what Browser are you using? – DigitalJedi Jun 11 '19 at 16:24
  • _This result is consistent across both the fiddle and the local html page on which I developed it_ What is the problem then? – weegee Jun 11 '19 at 16:24
  • @weegee as he stated, it doesn`t always work... not in the fiddle, nor on the local page. - therefor ist consistent - but not working ;) seems like a browser issue to me, because it DOES WORK 100% of the time in my browser. – DigitalJedi Jun 11 '19 at 16:27
  • @DigitalJedi am using Firefox 66 on Ubuntu 16.04. However, on further investigation, I cannot reproduce it either on Chrome or Firefox for Android. Perhaps it is a browser bug? Should I close the question? – Apocalypse Mystic Jun 11 '19 at 16:29
  • @Apocalypse Mystic - If you are OK with it, close the question... I definitly think its a browser thing... – DigitalJedi Jun 11 '19 at 16:30
  • @DigitalJedi do you want to submit that as an answer? I think that's what's going on here. Might as well leave it up I suppose in case someone else runs into it. – Apocalypse Mystic Jun 11 '19 at 16:43
  • I submitted an answer, as this really seems to be a browser issue... – DigitalJedi Jun 11 '19 at 16:58

1 Answers1

0
  1. Firefox:

I can upload as many fotos as I want, in any order I want, as many times in a row, and it is giving me no problems whatsoever

  1. Chrome:

When uploading a sequence like: pic1 - pic1 - pic1 - pic1, only the first image is uploaded.

When uploading a sequence like: pic1 - pic2 - pic1 - pic2, all the pictures are uploaded.

When uploading a sequence like: pic1 - pic1 - pic2 -pic2, the first instance of pic1 is uploaded, and the first instance of pic2 is uploaded

  1. IE

The same thing that happens in Chrome also goes for Internet Explorer...


I Dont think it`s a contenteditable issue, as it is very widly supported:

https://caniuse.com/#feat=contenteditable

This leads me to the conclusion that this is a browser issue.

DigitalJedi
  • 1,577
  • 1
  • 10
  • 29