Skip to content

Instantly share code, notes, and snippets.

@GeertVL-zz
Created August 22, 2013 05:43
Show Gist options
  • Save GeertVL-zz/6303579 to your computer and use it in GitHub Desktop.
Save GeertVL-zz/6303579 to your computer and use it in GitHub Desktop.
fileupload that works in html4 and html5
<!DOCTYPE html>
<html >
<head>
<title>Files property test</title>
<script type="text/javascript">
function reqListener() {
console.log('listened');
}
function getFiles() {
// Get input element
var myFileList = document.getElementById("myfiles");
var currentFile = 'file:///c:/temp/test.txt';
if (window.File && window.FileReader && window.FileList && window.Blob) {
var reader = new FileReader();
reader.onload = function(evt) {
console.log('onload');
//send data
evt.target.result
};
reader.readAsArrayBuffer(myFileList.files[0]);
} else {
var xhr = window.XMLHttpRequest && (window.location.protocol !== "file:" || !window.ActiveXObject) ?
function() {
return new window.XMLHttpRequest();
} :
function() {
try {
return new window.ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {}
};
var current = xhr();
var fileName = myFileList.value;
fileName = fileName.replace(/\\/g, '/');
fileName = 'file:///' + fileName;
current.open("GET", fileName, false);
current.send();
var filecontent = current.responseBody;
if (filecontent !== undefined)
{
console.log(filecontent);
}
}
}
</script>
</head>
<body>
<label>Use <strong>shift</strong> or <strong>ctrl</strong> click to pick a few files:
<input type="file" multiple id="myfiles" onchange="getFiles();" /></label>
<div id="display"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment