上传文件脚本控制格式大小.js
1 var iReporterID = 0; 2 var row = 0; 3 4 var $D = YAHOO.util.Dom; 5 var $E = YAHOO.util.Event; 6 var $C = YAHOO.util.Connect; 7 var $V = YAHOO.util.ValidationManager; 8 var $ = $D.get; 9 var $P = EGOVA.page.PageUtils; 10 var timer = null; 11 var callBackFn = null; 12 var AllowExt=".jpg"; 13 /** 14 * 初始化函数 15 */ 16 function init(){ 17 attachBtnEvent(); 18 $P.closeLoading(); 19 callBackFn = $("callBackFn").value; 20 } 21 22 /** 23 * 清除loading提示信息内容及样式 24 * @private 25 */ 26 var disappear = function() { 27 28 } 29 30 /** 31 * 按钮响应事件 32 */ 33 function attachBtnEvent() { 34 $E.on("saveBtn", "click", reporterInfoSave, this, true); 35 $E.on("resetBtn", "click", reporterInfoRest, this, true); 36 $E.on("returnBtn", "click", returnBtn, this, true); 37 } 38 39 /** 40 * 执行返回事件 41 */ 42 function returnBtn(){ 43 window.close(); 44 } 45 46 /** 47 * 执行新增事件 48 */ 49 function reporterInfoSave(){ 50 var fileText1=$("reportMediaForm").atth1.value.substring(reportMediaForm.atth1.value.length-4,reportMediaForm.atth1.value.length).toLowerCase(); 51 var fileText2=$("reportMediaForm").atth2.value.substring(reportMediaForm.atth2.value.length-4,reportMediaForm.atth2.value.length).toLowerCase(); 52 var fileText3=$("reportMediaForm").atth3.value.substring(reportMediaForm.atth3.value.length-4,reportMediaForm.atth3.value.length).toLowerCase(); 53 var fileText4=$("reportMediaForm").atth4.value.substring(reportMediaForm.atth4.value.length-4,reportMediaForm.atth4.value.length).toLowerCase(); 54 var mediaType = $("mediaType").value ; 55 if(AllowExt!=0&&AllowExt.indexOf($("atth1").value.substr($("atth1").value.lastIndexOf(".")).toLowerCase())==-1){ 56 var msg = "该文件类型不允许上传。请上传 "+AllowExt+" 类型的文件,当前文件类型为"+$("atth1").value.substr($("atth1").value.lastIndexOf(".")).toLowerCase(); 57 alert(msg); 58 return; 59 }else if(AllowExt!=0&&AllowExt.indexOf($("atth2").value.substr($("atth2").value.lastIndexOf(".")).toLowerCase())==-1){ 60 var msg = "该文件类型不允许上传。请上传 "+AllowExt+" 类型的文件,当前文件类型为"+$("atth2").value.substr($("atth2").value.lastIndexOf(".")).toLowerCase(); 61 alert(msg); 62 return; 63 }else if(AllowExt!=0&&AllowExt.indexOf($("atth3").value.substr($("atth3").value.lastIndexOf(".")).toLowerCase())==-1){ 64 var msg = "该文件类型不允许上传。请上传 "+AllowExt+" 类型的文件,当前文件类型为"+$("atth3").value.substr($("atth3").value.lastIndexOf(".")).toLowerCase(); 65 alert(msg); 66 return; 67 }else if(AllowExt!=0&&AllowExt.indexOf($("atth4").value.substr($("atth4").value.lastIndexOf(".")).toLowerCase())==-1){ 68 var msg = "该文件类型不允许上传。请上传 "+AllowExt+" 类型的文件,当前文件类型为"+$("atth4").value.substr($("atth4").value.lastIndexOf(".")).toLowerCase(); 69 alert(msg); 70 return; 71 } 72 var atthArray = []; 73 if($("atth1").value !=""){ 74 atthArray.push($("atth1").value); 75 } 76 if($("atth2").value !=""){ 77 atthArray.push($("atth2").value); 78 } 79 if($("atth3").value !=""){ 80 atthArray.push($("atth3").value); 81 } 82 if($("atth4").value !=""){ 83 atthArray.push($("atth4").value); 84 } 85 var fileTypes = $("fileTypes").value == null ? "" : $("fileTypes").value; 86 if(fileText1 == ‘‘&&fileText2 == ‘‘&&fileText3 == ‘‘&& fileText4==‘‘){ 87 alert("对不起,请选择要上传的多媒体文件。"); 88 return; 89 } 90 if( mediaType == 0 &&( fileText1 != ‘‘||fileText2 != ‘‘||fileText3!=‘‘||fileText4!=‘‘)) { 91 92 if(fileTypes != null && fileTypes != ""){ 93 if(fileText1 != ‘‘){ 94 if(fileTypes.indexOf(fileText1) < 0){ 95 alert("文件类型出错,支持的文件类型为:"+fileTypes); 96 return false; 97 } 98 99 } 100 if (fileText2 != ‘‘) { 101 if(fileTypes.indexOf(fileText2) < 0){ 102 alert("文件类型出错,支持的文件类型为:"+fileTypes); 103 return false; 104 } 105 106 } 107 if (fileText3 != ‘‘) { 108 if(fileTypes.indexOf(fileText3) < 0){ 109 alert("文件类型出错,支持的文件类型为:"+fileTypes); 110 return false; 111 } 112 113 } 114 if (fileText4 != ‘‘) { 115 if(fileTypes.indexOf(fileText4) < 0){ 116 alert("文件类型出错,支持的文件类型为:"+fileTypes); 117 return false; 118 } 119 120 } 121 } 122 } 123 var FileMaxSize = 100
124 125 var obj1=document.getElementById(‘atth1‘); 126 var obj2=document.getElementById(‘atth2‘); 127 var obj3=document.getElementById(‘atth3‘); 128 var obj4=document.getElementById(‘atth4‘); 129 var filePath1 = obj1.value; 130 var filePath2 = obj2.value; 131 var filePath3 = obj3.value; 132 var filePath4 = obj4.value; 133 var image1=new Image(); 134 var image2=new Image(); 135 var image3=new Image(); 136 var image4=new Image(); 137 image1.dynsrc=filePath1; 138 image2.dynsrc=filePath2; 139 image3.dynsrc=filePath3; 140 image4.dynsrc=filePath4; 141 filesize1=image1.fileSize; 142 filesize2=image2.fileSize; 143 filesize3=image3.fileSize; 144 filesize4=image4.fileSize; 145 if(filesize1>FileMaxSize*1024){ 146 alert("图片大小超过100K,请重新上传!"); 147 return false; 148 } 149 if(filesize2>FileMaxSize*1024){ 150 alert("图片大小超过100K,请重新上传!"); 151 return false; 152 } 153 if(filesize3>FileMaxSize*1024){ 154 alert("图片大小超过100K,请重新上传!"); 155 return false; 156 } 157 if(filesize4>FileMaxSize*1024){ 158 alert("图片大小超过100K,请重新上传!"); 159 return false; 160 } 161 $("saveBtn").disabled = true; 162 this.asyncRequest({ 163 url : "reportmediainfo.htm?EventID="+$("EventID").value+"&mediaType="+$("mediaType").value, 164 error : "上传图片失败!", 165 form : "reportMediaForm", 166 isUpload: true, 167 loadMessage:"正在上传图片信息......", 168 upload : function(o){ 169 var s = o.responseText.replace("<PRE>","").replace("</PRE>",""); 170 var jsonObject = s.parseJSON(); 171 if(jsonObject) { 172 var info = eval("jsonObject.ResultInfo"); 173 if(info.success=="true"){ 174 alert("上传成功!"); 175 $D.removeClass("loading", "OpenDlg_Loading"); 176 $("loading").innerText = ""; 177 $("reportMediaForm").reset(); 178 $("saveBtn").disabled = false; 179 if(callBackFn != null && callBackFn != ""){ 180 try{ 181 eval("window.dialogArguments."+callBackFn); 182 }catch(e){} 183 } 184 window.close(); 185 } else { 186 $("saveBtn").disabled = false; 187 msg = info.message?info.message:"上传失败!"; 188 if(info.message){ 189 $D.removeClass("loading", "OpenDlg_Loading"); 190 $D.addClass("loading", "OpenDlg_Error"); 191 $("loading").innerText = msg; 192 timer = setTimeout(disappear, 2000); 193 } 194 } 195 return (info.success=="true"); 196 } 197 } 198 }); 199 } 200 201 /** 202 * 异步获取数据 203 */ 204 function asyncRequest(option) { 205 var sUrl = option.url; 206 var that = this; 207 if(option.form) { 208 $C.setForm(option.form, option.isUpload, true); 209 } 210 var success = function(o) { 211 var html = o.responseText.toString() 212 if(html!="") { 213 if(!option.success(o)){ 214 failure(o); 215 } else { 216 disappear(); 217 } 218 } 219 else { 220 failure(o); 221 } 222 }; 223 var upload = function(){}; 224 if(option.isUpload){ upload = option.upload;} 225 var failure = function(o) { 226 $D.removeClass("loading", "OpenDlg_Loading"); 227 $D.addClass("loading", "OpenDlg_Error"); 228 $("loading").innerText = option.error; 229 timer = setTimeout(disappear, 2000); 230 if(option.failure){ option.failure(o);}; 231 }; 232 var callback = { 233 success : success, 234 failure : failure, 235 upload : upload 236 }; 237 clearTimeout(timer); 238 disappear(); 239 $D.addClass("loading", "OpenDlg_Loading"); 240 $("loading").innerText = option.loadMessage; 241 var req = $C.asyncRequest(‘POST‘, sUrl, callback); 242 } 243 244 /** 245 * 执行举报人信息重置 246 */ 247 function reporterInfoRest() { 248 $("reportMediaForm").reset(); 249 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。