Last active
August 29, 2015 13:56
-
-
Save chemzqm/8911101 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check for the various File API support. | |
if (window.File && window.FileReader && window.FileList && window.Blob) { | |
// Great success! All the File APIs are supported. | |
} else { | |
alert('The File APIs are not fully supported in this browser.'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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('<li><strong>', escape(f.name), '</strong> (', f.type || 'n/a', ') - ', | |
f.size, ' bytes, last modified: ', | |
f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a', | |
'</li>'); | |
} | |
document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>'; | |
} | |
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<style> | |
#progress_bar { | |
margin: 10px 0; | |
padding: 3px; | |
border: 1px solid #000; | |
font-size: 14px; | |
clear: both; | |
opacity: 0; | |
-moz-transition: opacity 1s linear; | |
-o-transition: opacity 1s linear; | |
-webkit-transition: opacity 1s linear; | |
} | |
#progress_bar.loading { | |
opacity: 1.0; | |
} | |
#progress_bar .percent { | |
background-color: #99ccff; | |
height: auto; | |
width: 0; | |
} | |
</style> | |
<input type="file" id="files" name="file" /> | |
<button onclick="abortRead();">Cancel read</button> | |
<div id="progress_bar"><div class="percent">0%</div></div> | |
<script> | |
var reader; | |
var progress = document.querySelector('.percent'); | |
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 updateProgress(evt) { | |
// evt is an 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 handleFileSelect(evt) { | |
// Reset progress indicator on new file selection. | |
progress.style.width = '0%'; | |
progress.textContent = '0%'; | |
reader = new FileReader(); | |
reader.onerror = errorHandler; | |
reader.onprogress = updateProgress; | |
reader.onabort = function(e) { | |
alert('File read cancelled'); | |
}; | |
reader.onloadstart = function(e) { | |
document.getElementById('progress_bar').className = 'loading'; | |
}; | |
reader.onload = function(e) { | |
// Ensure that the progress bar displays 100% at the end. | |
progress.style.width = '100%'; | |
progress.textContent = '100%'; | |
setTimeout("document.getElementById('progress_bar').className='';", 2000); | |
} | |
// Read in the image file as a binary string. | |
reader.readAsBinaryString(evt.target.files[0]); | |
} | |
document.getElementById('files').addEventListener('change', handleFileSelect, false); | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input type="file" id="files" name="files[]" multiple /> | |
<output id="list"></output> | |
<script> | |
function handleFileSelect(evt) { | |
var files = evt.target.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('<li><strong>', escape(f.name), '</strong> (', f.type || 'n/a', ') - ', | |
f.size, ' bytes, last modified: ', | |
f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a', | |
'</li>'); | |
} | |
document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>'; | |
} | |
document.getElementById('files').addEventListener('change', handleFileSelect, false); | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<style> | |
.thumb { | |
height: 75px; | |
border: 1px solid #000; | |
margin: 10px 5px 0 0; | |
} | |
</style> | |
<input type="file" id="files" name="files[]" multiple /> | |
<output id="list"></output> | |
<script> | |
function handleFileSelect(evt) { | |
var files = evt.target.files; // FileList object | |
// Loop through the FileList and render image files as thumbnails. | |
for (var i = 0, f; f = files[i]; i++) { | |
// Only process image files. | |
if (!f.type.match('image.*')) { | |
continue; | |
} | |
var reader = new FileReader(); | |
// Closure to capture the file information. | |
reader.onload = (function(theFile) { | |
return function(e) { | |
// Render thumbnail. | |
var span = document.createElement('span'); | |
span.innerHTML = ['<img class="thumb" src="', e.target.result, | |
'" title="', escape(theFile.name), '"/>'].join(''); | |
document.getElementById('list').insertBefore(span, null); | |
}; | |
})(f); | |
// Read in the image file as a data URL. | |
reader.readAsDataURL(f); | |
} | |
} | |
document.getElementById('files').addEventListener('change', handleFileSelect, false); | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>file upload test</title> | |
<style> | |
#drop { | |
margin: 40px; | |
height: 500px; | |
background-color: #eee; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="drop"> | |
</div> | |
<script type="text/javascript"> | |
if (!XMLHttpRequest.prototype.sendAsBinary) { | |
XMLHttpRequest.prototype.sendAsBinary = function(sData) { | |
var nBytes = sData.length, ui8Data = new Uint8Array(nBytes); | |
for (var nIdx = 0; nIdx < nBytes; nIdx++) { | |
ui8Data[nIdx] = sData.charCodeAt(nIdx) & 0xff; | |
} | |
/* send as ArrayBufferView...: */ | |
this.send(ui8Data); | |
/* ...or as ArrayBuffer (legacy)...: this.send(ui8Data.buffer); */ | |
}; | |
} | |
var el = document.getElementById('drop'); | |
el.addEventListener('drop', function(e) { | |
e.stopPropagation(); | |
e.preventDefault(); | |
var files = e.dataTransfer.files; | |
var f = files[0]; | |
var req = new XMLHttpRequest(); | |
req.onerror = function(e) { | |
throw e; | |
} | |
req.onload = function() { | |
console.log(req.responseText); | |
} | |
var reader = new FileReader(); | |
reader.onload = function() { | |
req.sendAsBinary(reader.result) | |
} | |
reader.readAsBinaryString(f); | |
req.open('post', 'http://localhost:3000/'); | |
req.addEventListener('progress', function(e) { | |
console.log(1); | |
}, false); | |
req.setRequestHeader('Content-Type', f.type); | |
}, false); | |
el.addEventListener('dragover', function(e) { | |
e.stopPropagation(); | |
e.preventDefault(); | |
e.dataTransfer.dropEffect = 'copy'; | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment