本文实例讲述了SWFUpload多文件上传及文件个数限制的方法。分享给大家供大家参考,具体如下:
SWFUpload是一个基于flash与JavaScript的客户端文件上传组件。
handlers.js文件
完成文件入列队(fileQueued) → 完成选择文件(fileDialogComplete) → 开始上传文件(uploadStart) → 上传处理(uploadProgress) → 上传成功(uploadSuccess) → 上传完成(uploadComplete) → 列队完成(queueComplete)
如上所示,单选文件顺序执行以上回调函数,需要注意的是选择多个文件,fileQueued、queueComplete只会执行一次,而fileDialogComplet...→ uploadComplete每个文件都执行一次。
参考官方例子完成基本功能后,仿赶集采用iframe的方式
thumbImages为父页面显示缩略图的div src_s为生成的缩略图地址 src_b为原图地址 serverData是图片上传处理页面返回的数据 成功则以 success|缩略图地址|原图地址 这样的格式返回
删除"; newA.href = "javascript::"; newA.onclick = function(){delDiv(newDiv);}; var newInput_s = parent.document.createElement("input"); newInput_s.type = "hidden"; newInput_s.value = src_s; newInput_s.name = "image_s[]"; newA.appendChild(newInput_s); var newInput_b = parent.document.createElement("input"); newInput_b.type = "hidden"; newInput_b.value = src_b; newInput_b.name = "image_b[]"; newA.appendChild(newInput_b); var newImgDiv = parent.document.createElement("div"); var newImg = parent.document.createElement("img"); newImg.style.height = "60px"; newImg.style.width = "80px"; newImgDiv.appendChild(newImg); newDiv.appendChild(newImgDiv); newDiv.appendChild(newA); parent.document.getElementById("thumbImages").appendChild(newDiv); if (newImg.filters) { try { newImg.filters.item("DXImageTransform.Microsoft.Alpha").opacity = 0; } catch (e) { // If it is not set initially,the browser will throw an error. This will set it if it is not set yet. newImg.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + 0 + ')'; } } else { newImg.style.opacity = 0; } newImg.onload = function () { fadeIn(newImg,0); }; newImg.src = src_s; } function fadeIn(element,opacity) { var reduceOpacityBy = 5; var rate = 30; // 15 fps if (opacity < 100) { opacity += reduceOpacityBy; if (opacity > 100) { opacity = 100; } if (element.filters) { try { element.filters.item("DXImageTransform.Microsoft.Alpha").opacity = opacity; } catch (e) { // If it is not set initially,the browser will throw an error. This will set it if it is not set yet. element.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + opacity + ')'; } } else { element.style.opacity = opacity / 100; } } if (opacity < 100) { setTimeout(function () { fadeIn(element,opacity); },rate); } }