我希望删除的文本文件按其内容或完整位置显示,以便我可以将该位置的内容加载到“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>