原生js实现多图片上传、预览




	
	多图上传、预览缩略图





	
	
11111 22222222

tinyImgUpload.js代码:

function tinyImgUpload(ele, options) {
    var eleList = document.querySelectorAll(ele);
    if(eleList.length == 0){
        console.log();
        return;
    }else if(eleList.length>1){
        console.log();
        return;
    }else {
        eleList[0].innerHTML ='
'+ '
+
'+ ''+ '
'; var ele = eleList[0].querySelector('#img-container'); ele.files = []; } var addBtn = document.querySelector('.img-up-add'); addBtn.addEventListener('click',function () { document.querySelector('#img-file-input').value = null; document.querySelector('#img-file-input').click(); return false; },false) function handleFileSelect(evt) { var files = evt.target.files; for(var i=0, f; f=files[i];i++){ if(!f.type.match('image.*')){ continue; } var tip = false; for(var j=0; j<(ele.files).length; j++){ if((ele.files)[j].name == f.name){ tip = true; break; } } if(!tip){ ele.files.push(f); var reader = new FileReader(); reader.onload = (function (theFile) { return function (e) { var oDiv = document.createElement('div'); oDiv.className = 'img-thumb img-item'; oDiv.innerHTML = ''+ 'x' ele.insertBefore(oDiv, addBtn); }; })(f); reader.readAsDataURL(f); } } } document.querySelector('#img-file-input').addEventListener('change', handleFileSelect, false); function removeImg(evt) { if(evt.target.className.match(/img-remove/)){ console.log('3',ele.files); function getIndex(ele){ if(ele && ele.nodeType && ele.nodeType == 1) { var oParent = ele.parentNode; var oChilds = oParent.children; for(var i = 0; i < oChilds.length; i++){ if(oChilds[i] == ele) return i; } }else { return -1; } } var index = getIndex(evt.target.parentNode); ele.removeChild(evt.target.parentNode); if(index < 0){ return; }else { ele.files.splice(index, 1); } console.log('4',ele.files); } } ele.addEventListener('click', removeImg, false); function uploadImg() { console.log(ele.files); var xhr = new XMLHttpRequest(); var formData = new FormData(); for(var i=0, f; f=ele.files[i]; i++){ formData.append('files', f); } console.log('1',ele.files); console.log('2',formData); xhr.onreadystatechange = function (e) { if(xhr.readyState == 4){ if(xhr.status == 200){ options.onSuccess(xhr.responseText); }else { options.onFailure(xhr.responseText); } } } xhr.open('POST', options.path, true); xhr.send(formData); } return uploadImg; }

 

你可能感兴趣的:(原生js实现多图片上传、预览)