1
1
/**
2
2
* simple ajax handler
3
3
**/
4
-
5
4
//ADD sendAsBinary compatibilty to older browsers
6
5
if ( XMLHttpRequest . prototype . sendAsBinary === undefined ) {
7
6
XMLHttpRequest . prototype . sendAsBinary = function ( string ) {
@@ -25,6 +24,7 @@ module.exports = function (method, url, headers, data, callback, err, isBinary)
25
24
binary = method ;
26
25
method = 'GET' ;
27
26
}
27
+ console . log ( data ) ;
28
28
method = method . toUpperCase ( ) ;
29
29
// Xhr.responseType 'json' is not supported in any of the vendors yet.
30
30
r . onload = function ( ) {
@@ -54,8 +54,19 @@ module.exports = function (method, url, headers, data, callback, err, isBinary)
54
54
if ( method === 'GET' || method === 'DELETE' ) {
55
55
data = null ;
56
56
} else if ( isBinary ) {
57
+ const keyData = data ;
57
58
const code = data . base64Code . replace ( 'data:' + data . type + ';base64,' , '' ) ;
58
- data = [ '--' + boundary , 'Content-Disposition: form-data; name="' + data . filed + '"; filename="' + data . filename + '"' , 'Content-Type: ' + data . type , '' , window . atob ( code ) , '--' + boundary + '--' ] . join ( '\r\n' ) ;
59
+ data = [ '--' + boundary , 'Content-Disposition: form-data; name="' + data . filed + '"; filename="' + data . filename + '"' , 'Content-Type: ' + data . type , '' , window . atob ( code ) , '' ] . join ( '\r\n' ) ;
60
+ const keyArr = Object . keys ( keyData ) ;
61
+ if ( keyArr . length > 4 ) {
62
+ for ( const k of keyArr ) {
63
+ if ( [ 'filed' , 'filename' , 'type' , 'base64Code' ] . indexOf ( k ) == - 1 ) {
64
+ data += [ '--' + boundary , 'Content-Disposition: form-data; name="' + k + '";' , '' , '' ] . join ( '\r\n' ) ;
65
+ data += [ typeof keyData [ k ] === 'object' ? JSON . stringify ( keyData [ k ] ) : keyData [ k ] , '' ] . join ( '\r\n' ) ;
66
+ }
67
+ }
68
+ }
69
+ data += '--' + boundary + '--' ;
59
70
}
60
71
// Open the path, async
61
72
r . open ( method , url , true ) ;
0 commit comments