@@ -219,39 +219,73 @@ var BaseUploader = (function () {
219
219
} , {
220
220
key : 'start' ,
221
221
value : function start ( ) {
222
- this . xhr = new XMLHttpRequest ( ) ;
223
- this . xhr . open ( 'POST' , this . profile . uploadUrl , true ) ;
222
+ var self = this ;
224
223
225
- this . onBeforeUpload ( ) ;
224
+ self . xhr = new XMLHttpRequest ( ) ;
225
+ self . xhr . open ( 'POST' , self . profile . uploadUrl , true ) ;
226
+
227
+ this . onBeforeUpload ( function ( ) {
228
+ _event_trigger2 [ 'default' ] . trigger ( self . element , 'BeforeUpload' , self . _detail ( ) ) ;
229
+
230
+ self . upload ( function ( ) {
231
+ _event_trigger2 [ 'default' ] . trigger ( self . element , 'FileUpload' , self . _detail ( ) ) ;
232
+ } ) ;
233
+ } ) ;
226
234
}
227
235
} , {
228
- key : 'upload' ,
229
- value : function upload ( ) {
230
- _event_trigger2 [ 'default' ] . trigger ( this . element , 'FileUpload' , this . _detail ( ) ) ;
231
- console . log ( 'File upload:' , this . file ) ;
236
+ key : 'onBeforeUpload' ,
237
+ value : function onBeforeUpload ( callback ) {
238
+ this . formData = new FormData ( ) ;
239
+ this . formData . append ( 'file' , this . file ) ;
240
+ this . formData . append ( 'name' , this . file . name ) ;
241
+ this . formData . append ( 'key' , this . file . name ) ;
242
+
243
+ callback ( ) ;
232
244
}
233
245
} , {
234
- key : 'cancel' ,
235
- value : function cancel ( ) { }
236
- } , {
237
- key : 'onBeforeUpload' ,
238
- value : function onBeforeUpload ( ) {
239
- _event_trigger2 [ 'default' ] . trigger ( this . element , 'BeforeUpload' , this . _detail ( ) ) ;
246
+ key : 'upload' ,
247
+ value : function upload ( callback ) {
248
+ var self = this ;
249
+
250
+ self . xhr . onload = function ( ) {
251
+ if ( self . xhr . status === 200 ) {
252
+ var data = JSON . parse ( self . xhr . responseText ) ;
253
+ self . onFileUploaded ( data , function ( ) {
254
+ var detail = self . _detail ( ) ;
255
+ detail . response = self . response ;
256
+
257
+ _event_trigger2 [ 'default' ] . trigger ( self . element , 'FileUploaded' , detail ) ;
258
+ } ) ;
259
+ } else {
260
+ self . onFileError ( self . xhr . status , self . xhr . responseText ) ;
261
+ }
262
+ } ;
263
+ self . xhr . send ( self . formData ) ;
240
264
241
- this . upload ( ) ;
265
+ callback ( ) ;
242
266
}
243
267
} , {
244
- key : 'onFileUpload ' ,
245
- value : function onFileUpload ( ) { }
268
+ key : 'cancel ' ,
269
+ value : function cancel ( ) { }
246
270
} , {
247
271
key : 'onFileProgress' ,
248
272
value : function onFileProgress ( ) { }
249
273
} , {
250
274
key : 'onFileUploaded' ,
251
- value : function onFileUploaded ( ) { }
275
+ value : function onFileUploaded ( data , callback ) {
276
+ this . response = data ;
277
+
278
+ callback ( ) ;
279
+ }
252
280
} , {
253
281
key : 'onFileError' ,
254
- value : function onFileError ( ) { }
282
+ value : function onFileError ( status , response ) {
283
+ var detail = this . _detail ( ) ;
284
+ detail . responseStatus = status ;
285
+ detail . responseText = response ;
286
+
287
+ _event_trigger2 [ 'default' ] . trigger ( this . element , 'FileError' , detail ) ;
288
+ }
255
289
} ] ) ;
256
290
257
291
return BaseUploader ;
@@ -262,6 +296,7 @@ module.exports = exports['default'];
262
296
263
297
} , { "../event_trigger" :2 } ] , 6 :[ function ( require , module , exports ) {
264
298
// http://developer.qiniu.com/docs/v6/api/overview/up/form-upload.html
299
+ // http://jssdk.demo.qiniu.io/
265
300
266
301
'use strict' ;
267
302
@@ -297,27 +332,47 @@ var QiniuUploader = (function (_BaseUploader) {
297
332
}
298
333
}
299
334
300
- _createClass ( QiniuUploader , [ {
301
- key : 'start' ,
302
- value : function start ( ) {
303
- var _this2 = this ;
304
-
305
- var _this = this ;
335
+ // Fetch upload token from profile.uptokenUrl
306
336
337
+ _createClass ( QiniuUploader , [ {
338
+ key : '_fetchUploadToken' ,
339
+ value : function _fetchUploadToken ( callback ) {
340
+ var self = this ;
307
341
var request = new XMLHttpRequest ( ) ;
342
+
308
343
request . open ( 'GET' , this . profile . uptokenUrl , true ) ;
309
344
request . onload = function ( ) {
310
345
if ( request . status === 200 ) {
311
346
var data = JSON . parse ( request . responseText ) ;
312
- _this . uptoken = data . uptoken ;
313
- console . log ( 'Uptoken:' , _this . uptoken ) ;
347
+ self . formData . append ( 'token' , data . uptoken ) ;
314
348
315
- _get ( Object . getPrototypeOf ( QiniuUploader . prototype ) , 'start' , _this2 ) . call ( _this2 ) ;
349
+ callback ( ) ;
316
350
}
317
351
} ;
318
352
319
353
request . send ( ) ;
320
354
}
355
+ } , {
356
+ key : 'onBeforeUpload' ,
357
+ value : function onBeforeUpload ( callback ) {
358
+ self = this ;
359
+
360
+ _get ( Object . getPrototypeOf ( QiniuUploader . prototype ) , 'onBeforeUpload' , this ) . call ( this , function ( ) {
361
+ self . _fetchUploadToken ( callback ) ;
362
+ } ) ;
363
+ }
364
+ } , {
365
+ key : 'onFileUploaded' ,
366
+ value : function onFileUploaded ( data , callback ) {
367
+ var self = this ;
368
+
369
+ _get ( Object . getPrototypeOf ( QiniuUploader . prototype ) , 'onFileUploaded' , this ) . call ( this , data , function ( ) {
370
+ var filename = encodeURIComponent ( self . response . key ) ;
371
+ self . response . url = self . profile . domain + filename ;
372
+
373
+ callback ( ) ;
374
+ } ) ;
375
+ }
321
376
} ] ) ;
322
377
323
378
return QiniuUploader ;
0 commit comments