Using drag and drop for selecting files by using JavaScript

by Subramanian 2012-12-18 19:15:11

Javascript to drag and drop files from the desktop to the browser...



<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++) {
output.push('', escape(f.name), ' (', f.type || 'n/a', ') - ',
f.size, ' bytes, last modified: ',
f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a',
'');
}
document.getElementById('list').innerHTML = '<ul>' + output.join('') + '';
}

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>



Note: Some browsers treat
<input type="file">
elements as native drop targets. Try dragging files onto the input field in this example.
998
like
0
dislike
0
mail
flag

You must LOGIN to add comments