/* url: 上传接口 (必传) data: 参数 (根据接口需求) success: 成功回调 error: 失败回调 type: 请求类型 (默认为POST) async: 是否为异步 (默认为true) timeout: 请求响应时间 (默认为60000) this.ajax.setResponseHeader("Content-Type", "application/x-www-form-urlencoded") 设置请求头 */ // 单文件上传 function Upload(url,data,success,error,type,async,timeout){ this.data = data; // 接口调用参数 this.url = url; // 数据请求地址 this.file = null; // 上传附件 this.ajax = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest(); this.type = type?type:"POST"; // 请求类型 默认POST this.async = async?async:true; // 是否为异步 默认true this.timeout = timeout?timeout:60000; // 请求响应时间 默认60000 this.inp = null; // 上传按钮 // 创建上传按钮 this.init = function(){ let inp = document.createElement("input"); inp.type = "file"; inp.name = "file"; inp.style.display = "none"; inp.onchange = this.getFile.bind(this); this.inp = inp; }; // 监听附件并调用上传方法 this.getFile = function(){ this.file = this.inp.files[0]; if(this.inp.accept && this.inp.accept!="*"){ let fileType = this.file.name.split(".")[this.file.name.split(".").length-1] if(this.inp.accept.indexOf(fileType)<0){ alert("请选择正确格式") return; } } this.uploadFile.call(this,success); }; // 附件上传 this.uploadFile = function(success){ if(!this.file){ return } let than = this; let form = new FormData(); // FormData 对象 form.append("file", this.file); // 文件对象 for(item in this.data){ form.append(item, this.data[item]); } // 上传进度 this.ajax.upload.onprogress = function(e){ } this.ajax.onerror = function(e){ if(error){ error(than.ajax) }else{ alert("上传失败") } } this.ajax.ontimeout = function(e){ alert("请求超时,请稍后重试") } this.ajax.timeout = this.timeout; this.ajax.open(this.type,url,this.async); this.ajax.send(form); this.ajax.onreadystatechange = function(){ if(than.ajax.readyState === 4){ if(than.ajax.status == 200){ if(success){ success(JSON.parse(than.ajax.responseText)) } }else{ if(error){ error(than.ajax) }else{ alert("上传失败") } } } than.inp.value = ""; } }; } /* url: 上传接口 (必传) data: 参数 (根据接口需求) success: 成功回调 error: 失败回调 type: 请求类型 (默认为POST) timeout: 请求响应时间 (默认为60000) this.ajax.setResponseHeader("Content-Type", "application/x-www-form-urlencoded") 设置请求头 */ // 多文件上传 function Uploads(url,data,success,error,type,timeout){ this.data = data; // 接口调用参数 this.url = url; // 数据请求地址 // this.file = null; // 上传附件 this.type = type?type:"POST"; // 请求类型 默认POST this.async = true; // 是否为异步 this.timeout = timeout?timeout:60000; // 请求响应时间 默认60000 this.inp = null; // 上传按钮 // 创建上传按钮 this.init = function(){ let inp = document.createElement("input"); inp.type = "file"; inp.name = "file"; inp.multiple="multiple" inp.style.display = "none"; inp.onchange = this.getFile.bind(this); this.inp = inp; }; // 监听附件并调用上传方法 this.getFile = function(){ let _this = this; for(let i=0;i<_this.inp.files.length;i++){ let uploadFile = _this.inp.files[i]; if(this.inp.accept && this.inp.accept!="*"){ let fileType = uploadFile.name.split(".")[uploadFile.name.split(".").length-1] if(this.inp.accept.indexOf(fileType)<0){ alert("请选择正确格式") return; } } let ajax = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest(); _this.uploadFile.call(_this,success,ajax,uploadFile); } }; // 附件上传 this.uploadFile = function(success,ajax,uploadFile){ if(!uploadFile){ return } let than = this; let form = new FormData(); // FormData 对象 form.append("file", uploadFile); // 文件对象 for(item in this.data){ form.append(item, this.data[item]); } // 上传进度 ajax.upload.onprogress = function(e){ } ajax.onerror = function(e){ if(error){ error(ajax) }else{ alert("上传失败") } } ajax.ontimeout = function(e){ alert("请求超时,请稍后重试") } ajax.timeout = this.timeout; ajax.open(this.type,url,this.async); ajax.send(form); ajax.onreadystatechange = function(){ if(ajax.readyState === 4){ if(ajax.status == 200){ if(success){ success(JSON.parse(ajax.responseText)) } }else{ if(error){ error(ajax) }else{ alert("上传失败") } } } than.inp.value = ""; } }; }