$(function(){ if (window.FileReader) { var file ; var list = document.getElementById('list'), cnt = document.getElementById('container'), btn = document.getElementById("upLoadbtn"); // 判断是否图片 function isImage(type) { switch (type) { case 'image/jpeg': case 'image/png': case 'image/gif': case 'image/bmp': case 'image/jpg': return true; default: return false; } }
function looks(f,img){ list.innerHTML += '
' + f.name + ' - ' + f.size + ' bytes
' + img + '
'; cnt.innerHTML = img;
} // 处理拖放文件列表 function handleFileSelect(evt) { evt.stopPropagation(); evt.preventDefault(); var files = evt.dataTransfer.files; for (var i = 0, f; f = files[i]; i++) { var t = f.type ? f.type : 'n/a'; reader = new FileReader(); isImg = isImage(t); // 处理得到的图片 if (isImg) { reader.onload = (function (theFile) { return function (e) { img = ''; looks(theFile, img); file=theFile; }; })(f); reader.readAsDataURL(f); } else { img = '"o((>ω< ))o",你传进来的不是图片!!'; looks(f, img); } } } // 处理插入拖出效果 function handleDragEnter(evt){ this.setAttribute('style', 'border-style:dashed;'); } function handleDragLeave(evt){ this.setAttribute('style', ''); }
var xhr = new XMLHttpRequest(); xhr.open('post', "test/upload.html", true); xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); var fd = new FormData(); fd.append('name', file.name); // Add file data fd.append('file', file); // Send data xhr.send(fd);