|
1 | | -/*global window, FileAPI */ |
| 1 | +/*global window, navigator, FileAPI */ |
2 | 2 |
|
3 | 3 | (function (api, window){ |
4 | 4 | "use strict"; |
|
8 | 8 | , FormData = window.FormData |
9 | 9 | , Form = function (){ this.items = []; } |
10 | 10 | , encodeURIComponent = window.encodeURIComponent |
| 11 | +, isPhantomJS = /phantomjs/i.test(navigator.userAgent)// @todo: fixed it |
11 | 12 | ; |
12 | 13 |
|
13 | 14 |
|
|
38 | 39 | api.log('FileAPI.Form.toHtmlData'); |
39 | 40 | this.toHtmlData(fn); |
40 | 41 | } |
41 | | -else if( this.multipart || !FormData ){ |
| 42 | +else if( isPhantomJS || this.multipart || !FormData ){ |
42 | 43 | api.log('FileAPI.Form.toMultipartData'); |
43 | 44 | this.toMultipartData(fn); |
44 | 45 | } |
|
94 | 95 | if( file.blob.toBlob ){ |
95 | 96 | // canvas |
96 | 97 | queue.inc(); |
97 | | -_converFile(file, function (file, blob){ |
| 98 | +_convertFile(file, function (file, blob){ |
98 | 99 | data.name = file.name; |
99 | 100 | data.file = blob; |
100 | 101 | data.size = blob.length; |
|
127 | 128 | this._to(new FormData, fn, function (file, data, queue){ |
128 | 129 | if( file.blob && file.blob.toBlob ){ |
129 | 130 | queue.inc(); |
130 | | -_converFile(file, function (file, blob){ |
| 131 | +_convertFile(file, function (file, blob){ |
131 | 132 | data.append(file.name, blob, file.file); |
132 | 133 | queue.next(); |
133 | 134 | }); |
|
149 | 150 | toMultipartData: function (fn){ |
150 | 151 | this._to([], fn, function (file, data, queue, boundary){ |
151 | 152 | queue.inc(); |
152 | | -_converFile(file, function (file, blob){ |
| 153 | +_convertFile(file, function (file, blob){ |
153 | 154 | data.push( |
154 | 155 | '--_' + boundary + ('\r\nContent-Disposition: form-data; name="'+ file.name +'"'+ (file.file ? '; filename="'+ encodeURIComponent(file.file) +'"' : '') |
155 | 156 | + (file.file ? '\r\nContent-Type: '+ (file.type || 'application/octet-stream') : '') |
|
158 | 159 | + '\r\n') |
159 | 160 | ); |
160 | 161 | queue.next(); |
161 | | -}); |
| 162 | +}, true); |
162 | 163 | }, api.expando); |
163 | 164 | } |
164 | 165 | }; |
165 | 166 |
|
166 | 167 |
|
167 | | -function _converFile(file, fn){ |
| 168 | +function _convertFile(file, fn, useBinaryString){ |
168 | 169 | var blob = file.blob, filename = file.file; |
169 | 170 |
|
170 | 171 | if( filename ){ |
|
193 | 194 | file.file = filename; |
194 | 195 | file.type = type; |
195 | 196 |
|
196 | | -if( blob.toBlob ){ |
| 197 | +if( !useBinaryString && blob.toBlob ){ |
197 | 198 | blob.toBlob(function (blob){ |
198 | 199 | fn(file, blob); |
199 | 200 | }, type, quality); |
|
0 commit comments