开发者

Drag a file from explorer and create a hyperlink into a dropped text area

i would like to drag a file from our windows file server and drop it onto a text area in webpage

the resulting link would be something like:

<a href="\\fileserver\folder\pizza_2.pdf">filename.pd开发者_运维百科f</a>


I think following code will help you:

<div id="drop_zone">Drop files here</div>
<output id="list"></output>

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

    var files = evt.dataTransfer.files; // FileList object.

    // files is a FileList of File objects. List some properties.
    var output = [];
    for (var i = 0, f; f = files[i]; i++) {
              //** you gotta change these lines accordingly **
      //output.push('<li><strong>', f.name, '</strong> (', f.type || 'n/a', ') - ',
        //        f.size, ' bytes, last modified: ',
        //        f.lastModifiedDate.toLocaleDateString(), '</li>');
    }
    document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';
  }

  function handleDragOver(evt) {
    evt.stopPropagation();
    evt.preventDefault();
  }

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

Reference: http://www.html5rocks.com/en/tutorials/file/dndfiles/#toc-selecting-files-dnd

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜