|
4 | 4 | "use strict"; |
5 | 5 |
|
6 | 6 | var |
7 | | - encode = window.encodeURIComponent |
8 | | - , document = window.document |
| 7 | + document = window.document |
9 | 8 | , FormData = window.FormData |
10 | 9 | , Form = function (){ this.items = []; } |
| 10 | + , encodeURIComponent = window.encodeURIComponent |
11 | 11 | ; |
12 | 12 |
|
13 | 13 |
|
|
90 | 90 | if( file.file ){ |
91 | 91 | data.type = file.file; |
92 | 92 | } |
| 93 | + |
93 | 94 | if( file.blob.toBlob ){ |
94 | 95 | // canvas |
95 | 96 | queue.inc(); |
96 | | - file.blob.toBlob(function (blob){ |
| 97 | + _converFile(file, function (file, blob){ |
97 | 98 | data.name = file.name; |
98 | 99 | data.file = blob; |
99 | 100 | data.size = blob.length; |
100 | 101 | data.type = file.type; |
101 | 102 | queue.next(); |
102 | | - }, 'image/png'); |
| 103 | + }); |
103 | 104 | } |
104 | 105 | else if( file.file ){ |
105 | | - //file |
| 106 | + // file |
106 | 107 | data.name = file.blob.name; |
107 | 108 | data.file = file.blob; |
108 | 109 | data.size = file.blob.size; |
109 | 110 | data.type = file.type; |
110 | 111 | } |
111 | 112 | else { |
112 | 113 | // additional data |
113 | | - if (!data.params) { |
| 114 | + if( !data.params ){ |
114 | 115 | data.params = []; |
115 | 116 | } |
116 | | - data.params.push(encodeURIComponent(file.name) + "=" + encodeURIComponent(file.blob)); |
| 117 | + data.params.push(encodeURIComponent(file.name) +"="+ encodeURIComponent(file.blob)); |
117 | 118 | } |
| 119 | + |
118 | 120 | data.start = -1; |
119 | 121 | data.end = -1; |
120 | 122 | data.retry = 0; |
|
123 | 125 |
|
124 | 126 | toFormData: function (fn){ |
125 | 127 | this._to(new FormData, fn, function (file, data, queue){ |
126 | | - if( file.file ){ |
127 | | - data.append('_'+file.name, file.file); |
128 | | - } |
129 | | - |
130 | 128 | if( file.blob && file.blob.toBlob ){ |
131 | 129 | queue.inc(); |
132 | | - file.blob.toBlob(function (blob){ |
| 130 | + _converFile(file, function (file, blob){ |
133 | 131 | data.append(file.name, blob, file.file); |
134 | 132 | queue.next(); |
135 | | - }, 'image/png'); |
| 133 | + }); |
136 | 134 | } |
137 | 135 | else if( file.file ){ |
138 | 136 | data.append(file.name, file.blob, file.file); |
139 | 137 | } |
140 | 138 | else { |
141 | 139 | data.append(file.name, file.blob); |
142 | 140 | } |
| 141 | + |
| 142 | + if( file.file ){ |
| 143 | + data.append('_'+file.name, file.file); |
| 144 | + } |
143 | 145 | }); |
144 | 146 | }, |
145 | 147 |
|
146 | 148 |
|
147 | 149 | toMultipartData: function (fn){ |
148 | 150 | this._to([], fn, function (file, data, queue, boundary){ |
149 | | - data.push( |
150 | | - '--_' + boundary + ('\r\nContent-Disposition: form-data; name="'+ file.name +'"'+ (file.file ? '; filename="'+ encode(file.file) +'"' : '') |
151 | | - + (file.file ? '\r\nContent-Type: '+ (file.type || 'application/octet-stream') : '') |
152 | | - + '\r\n' |
153 | | - + '\r\n'+ (file.file ? file.blob : encode(file.blob)) |
154 | | - + '\r\n') |
155 | | - ); |
| 151 | + _converFile(file, function (file, blob){ |
| 152 | + data.push( |
| 153 | + '--_' + boundary + ('\r\nContent-Disposition: form-data; name="'+ file.name +'"'+ (file.file ? '; filename="'+ encodeURIComponent(file.file) +'"' : '') |
| 154 | + + (file.file ? '\r\nContent-Type: '+ (file.type || 'application/octet-stream') : '') |
| 155 | + + '\r\n' |
| 156 | + + '\r\n'+ (file.file ? blob : encodeURIComponent(blob)) |
| 157 | + + '\r\n') |
| 158 | + ); |
| 159 | + }); |
156 | 160 | }, api.expando); |
157 | 161 | } |
158 | 162 | }; |
159 | 163 |
|
160 | 164 |
|
| 165 | + function _converFile(file, fn){ |
| 166 | + var blob = file.blob, filename = file.file; |
| 167 | + |
| 168 | + if( filename ){ |
| 169 | + var |
| 170 | + mime = { 'image/jpeg': '.jpe?g', 'image/png': '.png' } |
| 171 | + , type = mime[file.type] ? file.type : 'image/png' |
| 172 | + , ext = mime[type] || '.png' |
| 173 | + , quality = blob.quality || 1 |
| 174 | + ; |
| 175 | + |
| 176 | + if( !filename.match(new RegExp(ext+'$', 'i')) ){ |
| 177 | + filename += ext.replace('?', ''); |
| 178 | + } |
| 179 | + |
| 180 | + file.file = filename; |
| 181 | + file.type = type; |
| 182 | + |
| 183 | + if( blob.toBlob ){ |
| 184 | + blob.toBlob(function (blob){ |
| 185 | + fn(file, blob); |
| 186 | + }, type, quality); |
| 187 | + } |
| 188 | + else { |
| 189 | + blob = api.toBinaryString(blob.toDataURL(type, quality)); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + return blob; |
| 194 | + } |
| 195 | + |
| 196 | + |
161 | 197 | // @export |
162 | 198 | api.Form = Form; |
163 | 199 | })(FileAPI, window); |
0 commit comments