1
1
/**
2
2
* simple ajax handler
3
3
**/
4
- module . exports = function ( method , url , headers , data , callback , err ) {
4
+
5
+ //ADD sendAsBinary compatibilty to older browsers
6
+ if ( XMLHttpRequest . prototype . sendAsBinary === undefined ) {
7
+ XMLHttpRequest . prototype . sendAsBinary = function ( string ) {
8
+ var bytes = Array . prototype . map . call ( string , function ( c ) {
9
+ return c . charCodeAt ( 0 ) & 0xff ;
10
+ } ) ;
11
+ this . send ( new Uint8Array ( bytes ) . buffer ) ;
12
+ } ;
13
+ }
14
+
15
+ module . exports = function ( method , url , headers , data , callback , err , isBinary ) {
16
+
5
17
const r = new XMLHttpRequest ( ) ;
6
18
const error = err || function ( ) {
7
19
console . error ( 'AJAX ERROR!' ) ;
8
20
} ;
21
+ const boundary = 'vuecodeimageupload' ;
9
22
// Binary?
10
23
let binary = false ;
11
24
if ( method === 'blob' ) {
@@ -40,25 +53,9 @@ module.exports = function (method, url, headers, data, callback, err) {
40
53
// Should we add the query to the URL?
41
54
if ( method === 'GET' || method === 'DELETE' ) {
42
55
data = null ;
43
- }
44
- else if ( data && typeof ( data ) !== 'string' && ! ( data instanceof FormData ) && ! ( data instanceof File ) && ! ( data instanceof Blob ) ) {
45
- // Loop through and add formData
46
- var f = new FormData ( ) ;
47
- for ( x in data )
48
- if ( data . hasOwnProperty ( x ) ) {
49
- if ( data [ x ] instanceof HTMLInputElement ) {
50
- if ( 'files' in data [ x ] && data [ x ] . files . length > 0 ) {
51
- f . append ( x , data [ x ] . files [ 0 ] ) ;
52
- }
53
- }
54
- else if ( data [ x ] instanceof Blob ) {
55
- f . append ( x , data [ x ] , data . name ) ;
56
- }
57
- else {
58
- f . append ( x , data [ x ] ) ;
59
- }
60
- }
61
- data = f ;
56
+ } else if ( isBinary ) {
57
+ 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' ) ;
62
59
}
63
60
// Open the path, async
64
61
r . open ( method , url , true ) ;
@@ -75,6 +72,12 @@ module.exports = function (method, url, headers, data, callback, err) {
75
72
for ( x in headers ) {
76
73
r . setRequestHeader ( x , headers [ x ] ) ;
77
74
}
75
+ if ( isBinary ) {
76
+ r . setRequestHeader ( 'Content-Type' , 'multipart/form-data; boundary=' + boundary ) ;
77
+ }
78
+ }
79
+ if ( isBinary ) {
80
+ return r . sendAsBinary ( data ) ;
78
81
}
79
82
r . send ( data ) ;
80
83
return r ;
0 commit comments