File Selection, Drag & Drop, ...

HTML5 File API

HTML

<html>
	<div class="example">
        <input type="file" style="width: 0px; height: 0%;/*hide input button*/" id="files1" name="files1[]" multiple />
		<div id="drop_zone" onclick="dropzone=true; document.getElementById('files1').click();/*click on hidden input button onclick by this div*/">Drop files here or click to upload</div>
		<button onclick="abortRead();">Cancel loading files</button>
        <div id="progress_bar">
            <div class="percent">0%</div>
        </div>		
        <output id="file_list3"></output>
    </div>
</html>

CSS

.example {
    padding: 10px;
    border: 1px solid #ccc;
}
#drop_zone {
    border: 2px dashed #bbb;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    border-radius: 5px;
    padding: 25px;
    text-align: center;
    font: 20pt bold'Vollkorn';
    color: #bbb;
}
#drop_zone:hover {
    border: 2px dashed #666666;
    color: #666666;
}
.thumb {
    height: 75px;
    border: 1px solid #000;
    margin: 10px 5px 0 0;
}
#progress_bar {
    margin: 10px 0;
    padding: 3px;
    border: 1px solid #000;
    font-size: 14px;
    clear: both;
    opacity: 0;
    -o-transition: opacity 1s linear;
    -moz-transition: opacity 1s linear;
    -webkit-transition: opacity 1s linear;
    -ms-transition: opacity 1s linear;
}
#progress_bar.loading {
    opacity: 1.0;
}
#progress_bar .percent {
    background-color: #99ccff;
    height: auto;
    width: 0;
}
#byte_content {
    margin: 5px 0;
    max-height: 100px;
    overflow-y: auto;
    overflow-x: hidden;
}
#byte_range {
    margin-top: 5px;
}

JavaScript

var dropzone = false;	//is dropzone used? True or false. Default - false. True if used.
var total_bytes_uploaded = 0;
var limit_size = 512000;  // 500 KB * 1024 byte/KB = 512000 bytes.



// DROP ZONE
function handleFileSelect2(evt) {
	//total_bytes_uploaded = 0;
    evt.stopPropagation();
    evt.preventDefault();
	dropzone = true;
	handleFileSelect3(evt, dropzone);
	dropzone = false;
}

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

// Setup the Drag n' Drop listeners.
var dropZone = document.getElementById('drop_zone');
dropZone.addEventListener('dragover', handleDragOver, false);
dropZone.addEventListener('drop', handleFileSelect2, false);





// PROGRESS BAR
var reader;
var progress = document.querySelector('.percent');

function updateProgress(evt) {
    // evt is a ProgressEvent.
    if (evt.lengthComputable) {
        var percentLoaded = Math.round((evt.loaded / evt.total) * 100);
        // Increase the progress bar length.
        if (percentLoaded < 100) {
            progress.style.width = percentLoaded + '%';
            progress.textContent = percentLoaded + '%';
        }
    }
}

function abortRead() {
    reader.abort();
}

function errorHandler(evt) {
    switch (evt.target.error.code) {
        case evt.target.error.NOT_FOUND_ERR:
            alert('File Not Found!');
            break;
        case evt.target.error.NOT_READABLE_ERR:
            alert('File is not readable');
            break;
        case evt.target.error.ABORT_ERR:
            break; // noop
        default:
            alert('An error occurred reading this file.');
    }
}

function handleFileSelect3(evt, dropzone) {
	total_bytes_uploaded = 0;	//reset total bytes counting
	if(dropzone==true){
		var dropbox_files = evt.dataTransfer.files;	//evt.dataTransfer.files not available inside reader.onload, so store this here.
	}
    // Reset progress indicator on new...