Last active
August 29, 2015 14:07
-
-
Save webdesignberlin/87f76d468d87b7955888 to your computer and use it in GitHub Desktop.
alternate file if fileinput empty
This file contains 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
function handleFile(file){ | |
console.log(file) | |
} | |
document.querySelector('form').onsubmit = function(evt){ | |
evt.preventDefault(); | |
var fileField = document.querySelector('[name=file]'); | |
// Keine Datei gewählt? Default per XHR holen! | |
if(fileField.files.length === 0){ | |
var req = new XMLHttpRequest(); | |
req.open('GET', 'formtest.html'); | |
req.responseType = 'blob'; | |
req.onload = function(evt){ | |
var blob = evt.target.response; | |
handleFile(blob); | |
}; | |
req.send(); | |
} | |
// Gewählte Datei verarbeiten | |
else { | |
handleFile(fileField.files.item(0)) | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment