@@ -11,15 +11,9 @@ function xhrObj(type, opts, postData) {
11
11
this . type = type ;
12
12
this . opts = opts ;
13
13
this . postData = postData ;
14
- this . catch = opts . catch ;
15
- this . mobileHandle = opts . mobileHandle ;
14
+ this . catch = opts . catch || function ( ) { } ;
16
15
this . xmlHttp = window . XMLHttpRequest ? new XMLHttpRequest ( ) : new ActiveXObject ( 'Microsoft.XMLHTTP' ) ;
17
16
18
- if ( ! ! this . mobileHandle && typeof this . mobileHandle == 'function' ) {
19
- MOBLIE_CATCH . push ( this . xmlHttp ) ;
20
- this . mobileHandle ( ) ;
21
- }
22
-
23
17
this . xmlHttp . open ( opts . method , opts . url , opts . async ) ;
24
18
this . xmlHttp . withCredentials = opts . withCredentials ;
25
19
for ( var i in this . opts . headers ) {
@@ -100,16 +94,16 @@ function parseFileRes(evt) {
100
94
/*
101
95
xml上传文件
102
96
*/
103
- function XhrFile ( form , opt , dom ) {
97
+ function XhrFile ( opt , dom ) {
104
98
var _this = this ;
105
- this . xmlHttp = window . XMLHttpRequest ? new XMLHttpRequest ( ) : new ActiveXObject ( 'Microsoft.XMLHTTP' ) ;
106
99
this . dom = dom ;
100
+ this . opt = opt ;
101
+
102
+ this . xmlHttp = window . XMLHttpRequest ? new XMLHttpRequest ( ) : new ActiveXObject ( 'Microsoft.XMLHTTP' ) ;
107
103
this . xmlHttp . onload = uploadComplete ; // 请求完成
108
104
this . xmlHttp . onerror = uploadFailed ; // 请求失败
109
-
110
105
this . xmlHttp . upload . onprogress = progressFunction ; // 上传进度调用方法实现
111
106
112
-
113
107
this . onSuccessItem = function ( ) { } ;
114
108
this . onBeforeUploadItem = function ( ) { } ;
115
109
this . onErrorItem = function ( ) { } ;
@@ -120,29 +114,34 @@ function XhrFile(form, opt, dom) {
120
114
//上传开始执行方法
121
115
ot = new Date ( ) . getTime ( ) ; //设置上传开始时间
122
116
oloaded = 0 ; //设置上传开始时,以上传的文件大小为0
117
+ _this . fileItem . isUploadding = true ;
123
118
} ;
124
119
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
+ } ;
131
126
132
127
//上传成功响应
133
128
function uploadComplete ( evt ) {
134
129
//服务断接收完文件返回的结果
135
- console . log ( evt ) ;
136
130
var res = parseFileRes ( evt ) . res ;
137
131
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 ( ) ;
140
136
}
141
137
//上传失败
142
138
function uploadFailed ( evt ) {
143
139
var res = parseFileRes ( evt ) . res ;
144
140
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 ( ) ;
146
145
}
147
146
148
147
//上传进度实现方法,上传过程中会频繁调用该方法
@@ -155,7 +154,6 @@ function XhrFile(form, opt, dom) {
155
154
progressBar . value = evt . loaded ;
156
155
progress . percent = Math . round ( evt . loaded / evt . total * 100 ) ;
157
156
}
158
-
159
157
var nt = new Date ( ) . getTime ( ) ; //获取当前时间
160
158
var pertime = ( nt - ot ) / 1000 ; //计算出上次调用该方法时到现在的时间差,单位为s
161
159
ot = new Date ( ) . getTime ( ) ; //重新赋值时间,用于下次计算
@@ -180,22 +178,68 @@ function XhrFile(form, opt, dom) {
180
178
progress . resttime = resttime ;
181
179
182
180
if ( bspeed == 0 ) progress . bspeed = '上传已取消' ;
183
- _this . onProgressItem ( form , progress ) ;
181
+ _this . onProgressItem ( _this . fileItem , progress ) ;
184
182
}
185
183
}
186
184
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
+
187
226
// 取消上传
188
227
XhrFile . prototype . cancleUploadFile = function ( ) {
189
- console . log ( "cancleUploadFile" ) ;
228
+ _this . fileItem . isCancel = true ;
229
+ _this . fileItem . isUploadding = false ;
230
+ _this . fileItem . isUpload = false ;
190
231
this . xmlHttp . abort ( ) ;
232
+ this . clearUploadFile ( ) ;
191
233
} ;
192
234
// 清除文件
193
235
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
+ }
195
241
} ;
196
242
197
- window . MOBLIE_CATCH = [ ] ; //预留mobile拦截数组
198
-
199
243
/*
200
244
ajax构造函数
201
245
*/
@@ -216,9 +260,6 @@ function Ajax() {
216
260
217
261
// 注册拦截函数
218
262
this . catch = function ( ) { } ;
219
-
220
- // 移动端请求句柄
221
- this . mobileHandle = null ;
222
263
}
223
264
224
265
/*
@@ -236,7 +277,6 @@ Ajax.prototype.creatOpts = function (opts) {
236
277
_opts . method = opts . method && opts . method . toUpperCase ( ) ;
237
278
238
279
_opts . catch = this . catch ;
239
- _opts . mobileHandle = this . mobileHandle ;
240
280
return _opts ;
241
281
} ;
242
282
@@ -253,6 +293,7 @@ Ajax.prototype.ajax = function (_opts) {
253
293
return xml ;
254
294
}
255
295
opts . url = this . opts . baseUrl + opts . url ;
296
+ console . log ( xmlHttp ) ;
256
297
return new xhrObj ( opts , xmlHttp ) ;
257
298
} ;
258
299
@@ -270,7 +311,7 @@ Ajax.prototype.config = function (opts) {
270
311
} ;
271
312
272
313
// 请求方法集合
273
- var methods_1 = [ 'get' , 'delete ' , 'head' , 'options' ] ; //不带data
314
+ var methods_1 = [ 'get' , 'del ' , 'head' , 'options' ] ; //不带data
274
315
var methods_2 = [ 'post' , 'put' , 'patch' ] ; //带data
275
316
for ( var i in methods_1 ) {
276
317
! function ( methods_1 , i ) {
@@ -302,13 +343,13 @@ for (var j in methods_2) {
302
343
/*
303
344
文件上传
304
345
*/
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 ;
307
349
308
- var form = new FormData ( ) ;
309
- form . append ( "file" , fileObj ) ; // 文件对象
350
+ opt . url = this . opts . baseUrl + opt . url ;
310
351
311
- return new XhrFile ( form , opt , dom ) ;
352
+ return new XhrFile ( opt , dom ) ;
312
353
} ;
313
354
314
355
// 检验是否浏览器环境
0 commit comments