Skip to content

Instantly share code, notes, and snippets.

@ShawnSWu
Created January 3, 2018 02:34
Show Gist options
  • Select an option

  • Save ShawnSWu/1dcbb38312c041dca8cf927868912b7d to your computer and use it in GitHub Desktop.

Select an option

Save ShawnSWu/1dcbb38312c041dca8cf927868912b7d to your computer and use it in GitHub Desktop.
<script type='text/javascript'>
function _id(e){return document.getElementById(e)}
_id('myfile').onchange = function(){
var theFile = this.files;
if(theFile.length === 1){
var uploader = new XMLHttpRequest();
var file = new FormData();
uploader.onreadystatechange = function(){
if(uploader.readyState ===4 && uploader.status === 200){
console.log(uploader.responseTeXt);
}
}
uploader.open('POST','uploadfile.php',true);
uploader.send(file);
alert('上傳1個檔案成功');
}else{
var start = 0,
setter,
buff=true;
setter = setInterval(function(){
if(buff === true){
buff = false;
var uploader = new XMLHttpRequest();
var file = new FormData();
file.append('file',theFile[start]);
uploader.onreadystatechange = function(){
if(uploader.readyState === 4 && uploader.status === 200){
console.log(uploader.responseTeXt);
clearInterval(setter);
}
}
alert('上傳'+theFile.length+'個檔案成功');
uploader.open('POST','uploadfile.php',true);
uploader.send(file);
}
},500);
}
window.location.reload();
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment