@ -1,6 +1,6 @@
/ * !
/ * !
* Name : vue - upload - component
* Name : vue - upload - component
* Version : 2.8 . 0
* Version : 2.8 . 1
* Author : LianYue
* Author : LianYue
* /
* /
( function ( global , factory ) {
( function ( global , factory ) {
@ -39,16 +39,43 @@ var sendRequest = function sendRequest(xhr, body) {
if ( xhr . status >= 200 && xhr . status < 300 ) {
if ( xhr . status >= 200 && xhr . status < 300 ) {
resolve ( xhr . response ) ;
resolve ( xhr . response ) ;
} else {
} else {
reject ( xhr . statusText ) ;
reject ( xhr . response ) ;
}
}
} ;
} ;
xhr . onerror = function ( ) {
xhr . onerror = function ( ) {
return reject ( xhr . statusText ) ;
return reject ( xhr . response ) ;
} ;
} ;
xhr . send ( JSON . stringify ( body ) ) ;
xhr . send ( JSON . stringify ( body ) ) ;
} ) ;
} ) ;
} ;
} ;
/ * *
* Sends a XHR request with certain form data
*
* @ param { XMLHttpRequest } xhr
* @ param { Object } data
* /
var sendFormRequest = function sendFormRequest ( xhr , data ) {
var body = new FormData ( ) ;
for ( var name in data ) {
body . append ( name , data [ name ] ) ;
}
return new Promise ( function ( resolve , reject ) {
xhr . onload = function ( ) {
if ( xhr . status >= 200 && xhr . status < 300 ) {
resolve ( xhr . response ) ;
} else {
reject ( xhr . response ) ;
}
} ;
xhr . onerror = function ( ) {
return reject ( xhr . response ) ;
} ;
xhr . send ( body ) ;
} ) ;
} ;
/ * *
/ * *
* Creates and sends XHR request
* Creates and sends XHR request
*
*
@ -182,7 +209,9 @@ var ChunkUploadHandler = function () {
request ( {
request ( {
method : 'POST' ,
method : 'POST' ,
headers : this . headers ,
headers : Object . assign ( this . headers , {
'Content-Type' : 'application/json'
} ) ,
url : this . action ,
url : this . action ,
body : Object . assign ( this . startBody , {
body : Object . assign ( this . startBody , {
phase : 'start' ,
phase : 'start' ,
@ -191,7 +220,8 @@ var ChunkUploadHandler = function () {
} )
} )
} ) . then ( function ( res ) {
} ) . then ( function ( res ) {
if ( res . status !== 'success' ) {
if ( res . status !== 'success' ) {
return _this2 . reject ( res . message ) ;
_this2 . file . response = res ;
return _this2 . reject ( 'server' ) ;
}
}
_this2 . sessionId = res . data . session _id ;
_this2 . sessionId = res . data . session _id ;
@ -199,8 +229,9 @@ var ChunkUploadHandler = function () {
_this2 . createChunks ( ) ;
_this2 . createChunks ( ) ;
_this2 . startChunking ( ) ;
_this2 . startChunking ( ) ;
} ) . catch ( function ( error ) {
} ) . catch ( function ( res ) {
return _this2 . reject ( error ) ;
_this2 . file . response = res ;
_this2 . reject ( 'server' ) ;
} ) ;
} ) ;
}
}
@ -256,7 +287,9 @@ var ChunkUploadHandler = function () {
this . updateFileProgress ( ) ;
this . updateFileProgress ( ) ;
chunk . xhr = createRequest ( {
chunk . xhr = createRequest ( {
method : 'POST' ,
method : 'POST' ,
headers : this . headers ,
headers : Object . assign ( this . headers , {
'Content-Type' : 'multipart/form-data'
} ) ,
url : this . action
url : this . action
} ) ;
} ) ;
@ -266,7 +299,7 @@ var ChunkUploadHandler = function () {
}
}
} , false ) ;
} , false ) ;
send Request( chunk . xhr , Object . assign ( this . uploadBody , {
send Form Request( chunk . xhr , Object . assign ( this . uploadBody , {
phase : 'upload' ,
phase : 'upload' ,
session _id : this . sessionId ,
session _id : this . sessionId ,
start _offset : chunk . startOffset ,
start _offset : chunk . startOffset ,
@ -278,7 +311,7 @@ var ChunkUploadHandler = function () {
} else {
} else {
if ( chunk . retries -- <= 0 ) {
if ( chunk . retries -- <= 0 ) {
_this3 . pause ( ) ;
_this3 . pause ( ) ;
return _this3 . reject ( ' File upload faile d') ;
return _this3 . reject ( ' upload') ;
}
}
}
}
@ -287,7 +320,7 @@ var ChunkUploadHandler = function () {
chunk . active = false ;
chunk . active = false ;
if ( chunk . retries -- <= 0 ) {
if ( chunk . retries -- <= 0 ) {
_this3 . pause ( ) ;
_this3 . pause ( ) ;
return _this3 . reject ( ' File upload faile d') ;
return _this3 . reject ( ' upload') ;
}
}
_this3 . uploadNextChunk ( ) ;
_this3 . uploadNextChunk ( ) ;
@ -308,20 +341,24 @@ var ChunkUploadHandler = function () {
request ( {
request ( {
method : 'POST' ,
method : 'POST' ,
headers : this . headers ,
headers : Object . assign ( this . headers , {
'Content-Type' : 'application/json'
} ) ,
url : this . action ,
url : this . action ,
body : Object . assign ( this . finishBody , {
body : Object . assign ( this . finishBody , {
phase : 'finish' ,
phase : 'finish' ,
session _id : this . sessionId
session _id : this . sessionId
} )
} )
} ) . then ( function ( res ) {
} ) . then ( function ( res ) {
_this4 . file . response = res ;
if ( res . status !== 'success' ) {
if ( res . status !== 'success' ) {
return _this4 . reject ( res . message ) ;
return _this4 . reject ( 'server' ) ;
}
}
_this4 . resolve ( res ) ;
_this4 . resolve ( res ) ;
} ) . catch ( function ( error ) {
} ) . catch ( function ( res ) {
return _this4 . reject ( error ) ;
_this4 . file . response = res ;
_this4 . reject ( 'server' ) ;
} ) ;
} ) ;
}
}
} , {
} , {
@ -594,6 +631,10 @@ var FileUpload = { render: function render() {
type : String
type : String
} ,
} ,
customAction : {
type : Function
} ,
headers : {
headers : {
type : Object ,
type : Object ,
default : Object
default : Object
@ -1254,6 +1295,10 @@ var FileUpload = { render: function render() {
return Promise . reject ( 'size' ) ;
return Promise . reject ( 'size' ) ;
}
}
if ( this . customAction ) {
return this . customAction ( file , this ) ;
}
if ( this . features . html5 ) {
if ( this . features . html5 ) {
if ( this . shouldUseChunkUpload ( file ) ) {
if ( this . shouldUseChunkUpload ( file ) ) {
return this . uploadChunk ( file ) ;
return this . uploadChunk ( file ) ;
@ -1261,11 +1306,14 @@ var FileUpload = { render: function render() {
if ( file . putAction ) {
if ( file . putAction ) {
return this . uploadPut ( file ) ;
return this . uploadPut ( file ) ;
}
}
if ( file . postAction ) {
return this . uploadHtml5 ( file ) ;
return this . uploadHtml5 ( file ) ;
}
}
}
if ( file . postAction ) {
return this . uploadHtml4 ( file ) ;
return this . uploadHtml4 ( file ) ;
}
return Promise . reject ( 'No action configured' ) ;
} ,
} ,