Drag and drop images, and not links, between windows - HTML5

后端 未结 1 1431
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-06 14:01

I\'m trying to make a drag and drop uploader in HTML5 with the added requirement of being able to do so with files dragged from other websites I don\'t own and can\'t edit (

相关标签:
1条回答
  • 2020-12-06 15:00

    At dragover event handler, iterating event.dataTransfer.types

    The DataTransfer.types read-only property is an array of the drag data formats (as strings) that were set in the dragstart event. The order of the formats is the same order as the data included in the drag operation.

    the array contains three types:

    • text/plain
    • text/uri-list
    • text/html

    text/html is the html string of the dragged <img> element.

    Get the string using by passing "text/html" to .getData(), then either extract src of html string using RegExp or create an element and append the html string to the element, then get src of <img> element using .src.

    holder.ondrop = function (e) {
      e.preventDefault();       
      this.className = '';
      var img = e.dataTransfer.getData("text/html");
      var div = document.createElement("div");
      div.innerHTML = img;
      var src = div.firstChild.src;
      console.log(div.firstChild, src);
      results.innerText = src;
    }
    

    jsfiddle https://jsfiddle.net/2m58rfou/6/

    0 讨论(0)
提交回复
热议问题