Skip to content

Commit d8c7cf4

Browse files
committed
2 parents 7293cdd + 2e2cc56 commit d8c7cf4

File tree

10 files changed

+226
-145
lines changed

10 files changed

+226
-145
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,15 @@ uploader.onErrorItem = function(fileItem, res, status){
7777
uploader.onProgressItem = function(fileItem, progress){
7878
// 正在上传回调
7979
}
80+
uploader.upload() // 开始上传
8081
```
8182
## uploader opt
8283

8384
```js
8485
var opt = {
8586
url : url, // 上传地址
8687
isUploadClear : false, // 上传成功或失败后是否清除文件对象
87-
data : data // 上传附带参数
88+
data : data, // 上传附带参数
89+
autoUpload : false // 是否添加后自动上传
8890
}
8991
```

dist/ajax.js

+78-37
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,9 @@ function xhrObj(type, opts, postData) {
1111
this.type = type;
1212
this.opts = opts;
1313
this.postData = postData;
14-
this.catch = opts.catch;
15-
this.mobileHandle = opts.mobileHandle;
14+
this.catch = opts.catch || function () {};
1615
this.xmlHttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
1716

18-
if (!!this.mobileHandle && typeof this.mobileHandle == 'function') {
19-
MOBLIE_CATCH.push(this.xmlHttp);
20-
this.mobileHandle();
21-
}
22-
2317
this.xmlHttp.open(opts.method, opts.url, opts.async);
2418
this.xmlHttp.withCredentials = opts.withCredentials;
2519
for (var i in this.opts.headers) {
@@ -100,16 +94,16 @@ function parseFileRes(evt) {
10094
/*
10195
xml上传文件
10296
*/
103-
function XhrFile(form, opt, dom) {
97+
function XhrFile(opt, dom) {
10498
var _this = this;
105-
this.xmlHttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
10699
this.dom = dom;
100+
this.opt = opt;
101+
102+
this.xmlHttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
107103
this.xmlHttp.onload = uploadComplete; // 请求完成
108104
this.xmlHttp.onerror = uploadFailed; // 请求失败
109-
110105
this.xmlHttp.upload.onprogress = progressFunction; // 上传进度调用方法实现
111106

112-
113107
this.onSuccessItem = function () {};
114108
this.onBeforeUploadItem = function () {};
115109
this.onErrorItem = function () {};
@@ -120,29 +114,34 @@ function XhrFile(form, opt, dom) {
120114
//上传开始执行方法
121115
ot = new Date().getTime(); //设置上传开始时间
122116
oloaded = 0; //设置上传开始时,以上传的文件大小为0
117+
_this.fileItem.isUploadding = true;
123118
};
124119

125-
setTimeout(function () {
126-
_this.onBeforeUploadItem(form);
127-
128-
_this.xmlHttp.open("post", opt.url, true); //post方式,url为服务器请求地址,true 该参数规定请求是否异步处理。
129-
_this.xmlHttp.send(form); //开始上传,发送form数据
130-
}, 100);
120+
// 给input绑定change事件
121+
this.dom.onchange = function () {
122+
if (_this.opt.autoUpload || false) {
123+
_this.upload();
124+
}
125+
};
131126

132127
//上传成功响应
133128
function uploadComplete(evt) {
134129
//服务断接收完文件返回的结果
135-
console.log(evt);
136130
var res = parseFileRes(evt).res;
137131
var status = parseFileRes(evt).status;
138-
_this.onSuccessItem(form, res, status);
139-
// dom.value = "";
132+
_this.fileItem.isUpload = true;
133+
_this.fileItem.isUploadding = false;
134+
_this.onSuccessItem(_this.fileItem, res, status);
135+
_this.clearUploadFile();
140136
}
141137
//上传失败
142138
function uploadFailed(evt) {
143139
var res = parseFileRes(evt).res;
144140
var status = parseFileRes(evt).status;
145-
_this.onErrorItem(form, res, status);
141+
_this.fileItem.isError = true;
142+
_this.fileItem.isUploadding = false;
143+
_this.onErrorItem(_this.fileItem, res, status);
144+
_this.clearUploadFile();
146145
}
147146

148147
//上传进度实现方法,上传过程中会频繁调用该方法
@@ -155,7 +154,6 @@ function XhrFile(form, opt, dom) {
155154
progressBar.value = evt.loaded;
156155
progress.percent = Math.round(evt.loaded / evt.total * 100);
157156
}
158-
159157
var nt = new Date().getTime(); //获取当前时间
160158
var pertime = (nt - ot) / 1000; //计算出上次调用该方法时到现在的时间差,单位为s
161159
ot = new Date().getTime(); //重新赋值时间,用于下次计算
@@ -180,22 +178,68 @@ function XhrFile(form, opt, dom) {
180178
progress.resttime = resttime;
181179

182180
if (bspeed == 0) progress.bspeed = '上传已取消';
183-
_this.onProgressItem(form, progress);
181+
_this.onProgressItem(_this.fileItem, progress);
184182
}
185183
}
186184

185+
// 开始上传
186+
XhrFile.prototype.upload = function () {
187+
if (!this.dom.value) return;
188+
var _this = this;
189+
190+
var fileObj = this.dom.files[0]; // js 获取文件对象
191+
var form = new FormData();
192+
form.append("file", fileObj); // 文件对象
193+
form.append("parm", JSON.stringify(this.opt.data)); // 文件对象添加额外参数
194+
195+
// 构建fileItem
196+
var fileItem = {
197+
formData: form,
198+
url: this.opt.url || "",
199+
data: this.opt.data || "",
200+
addr: this.dom.value || "",
201+
isUpload: false,
202+
isCancel: false,
203+
isUploadding: false,
204+
isError: false,
205+
isUploadClear: this.opt.isUploadClear || false,
206+
autoUpload: this.opt.autoUpload || false
207+
};
208+
209+
this.fileItem = fileItem;
210+
211+
// 怎么延时确保自定义函数已绑定
212+
setTimeout(function () {
213+
_this.onBeforeUploadItem(_this.fileItem);
214+
_this.xmlHttp.open("post", _this.fileItem.url, true); //post方式,url为服务器请求地址,true 该参数规定请求是否异步处理。
215+
216+
if (_this.opt.headers) {
217+
for (var i in _this.opt.headers) {
218+
_this.xmlHttp.setRequestHeader(i, _this.opt.headers[i]);
219+
}
220+
}
221+
222+
_this.xmlHttp.send(_this.fileItem.formData); //开始上传,发送form数据
223+
}, 20);
224+
};
225+
187226
// 取消上传
188227
XhrFile.prototype.cancleUploadFile = function () {
189-
console.log("cancleUploadFile");
228+
_this.fileItem.isCancel = true;
229+
_this.fileItem.isUploadding = false;
230+
_this.fileItem.isUpload = false;
190231
this.xmlHttp.abort();
232+
this.clearUploadFile();
191233
};
192234
// 清除文件
193235
XhrFile.prototype.clearUploadFile = function () {
194-
this.dom.value = "";
236+
if (this.fileItem.isUploadClear) {
237+
// this.dom.outerHTML = this.dom.outerHTML;
238+
this.dom.value = "";
239+
this.dom.files[0] = null;
240+
}
195241
};
196242

197-
window.MOBLIE_CATCH = []; //预留mobile拦截数组
198-
199243
/*
200244
ajax构造函数
201245
*/
@@ -216,9 +260,6 @@ function Ajax() {
216260

217261
// 注册拦截函数
218262
this.catch = function () {};
219-
220-
// 移动端请求句柄
221-
this.mobileHandle = null;
222263
}
223264

224265
/*
@@ -236,7 +277,6 @@ Ajax.prototype.creatOpts = function (opts) {
236277
_opts.method = opts.method && opts.method.toUpperCase();
237278

238279
_opts.catch = this.catch;
239-
_opts.mobileHandle = this.mobileHandle;
240280
return _opts;
241281
};
242282

@@ -253,6 +293,7 @@ Ajax.prototype.ajax = function (_opts) {
253293
return xml;
254294
}
255295
opts.url = this.opts.baseUrl + opts.url;
296+
console.log(xmlHttp);
256297
return new xhrObj(opts, xmlHttp);
257298
};
258299

@@ -270,7 +311,7 @@ Ajax.prototype.config = function (opts) {
270311
};
271312

272313
// 请求方法集合
273-
var methods_1 = ['get', 'delete', 'head', 'options']; //不带data
314+
var methods_1 = ['get', 'del', 'head', 'options']; //不带data
274315
var methods_2 = ['post', 'put', 'patch']; //带data
275316
for (var i in methods_1) {
276317
!function (methods_1, i) {
@@ -302,13 +343,13 @@ for (var j in methods_2) {
302343
/*
303344
文件上传
304345
*/
305-
Ajax.prototype.uploader = function (dom, opt) {
306-
var fileObj = dom.files[0]; // js 获取文件对象
346+
Ajax.prototype.uploader = function (id, opt) {
347+
var dom = document.getElementById(id);
348+
if (dom.type !== "file") return;
307349

308-
var form = new FormData();
309-
form.append("file", fileObj); // 文件对象
350+
opt.url = this.opts.baseUrl + opt.url;
310351

311-
return new XhrFile(form, opt, dom);
352+
return new XhrFile(opt, dom);
312353
};
313354

314355
// 检验是否浏览器环境

dist/ajax.min.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)