html5 – 拖动删除文本文件

前端之家收集整理的这篇文章主要介绍了html5 – 拖动删除文本文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我希望删除的文本文件按其内容或完整位置显示,以便我可以将该位置的内容加载到“drop_zone”中.

这就是我现在所拥有的.我只能访问文件名:

<html>
<head>
  <title></title>
</head>
<body>
<textarea id="drop_zone">Drop files here</textarea>
<output id="list"></output>

<script>
  function handleFileSelect(evt) {
    evt.stopPropagation();
    evt.preventDefault();

    var files = evt.dataTransfer.files; // FileList object.
    document.getElementById('drop_zone').innerHTML = files[0].name;
  }

  function handleDragOver(evt) {
    evt.stopPropagation();
    evt.preventDefault();
    evt.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
  }

  // Setup the dnd listeners.
  var dropZone = document.getElementById('drop_zone');
  dropZone.addEventListener('dragover',handleDragOver,false);
  dropZone.addEventListener('drop',handleFileSelect,false);
</script>
</body>
</html>

解决方法

这是最终的代码
<html>
<head>
  <title></title>
</head>
<body>
<textarea id="drop_zone">Drop files here</textarea>
<script>
  function handleFileSelect(evt) {
    evt.stopPropagation();
    evt.preventDefault();

    var files = evt.dataTransfer.files; // FileList object.
    var reader = new FileReader();  
    reader.onload = function(event) {            
         document.getElementById('drop_zone').value = event.target.result;
    }        
    reader.readAsText(files[0],"UTF-8");
  }

  function handleDragOver(evt) {
    evt.stopPropagation();
    evt.preventDefault();
    evt.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
  }

  // Setup the dnd listeners.
  var dropZone = document.getElementById('drop_zone');
  dropZone.addEventListener('dragover',false);
</script>
</body>
</html>
原文链接:https://www.f2er.com/html5/168254.html

猜你在找的HTML5相关文章