Skip to content
This repository
Browse code

Wrap files array in an object for the JSON response.

Sample response:
{"files":[{"name":"cat.jpg","size":464885,"url":"http:\/\/example.org\/f
iles\/cat.jpg","thumbnail_url":"http:\/\/example.org\/files\/thumbnail\/
cat.jpg","delete_url":"http:\/\/example.org\/files\/cat.jpg","delete_typ
e":"DELETE"}]}
  • Loading branch information...
commit fb455d8fe8365b0f4e8df84d7d6bdd6b739e0660 1 parent 41d7d61
Sebastian Tschan authored December 12, 2012
21  js/jquery.fileupload-ui.js
... ...
@@ -1,5 +1,5 @@
1 1
 /*
2  
- * jQuery File Upload User Interface Plugin 6.11.1
  2
+ * jQuery File Upload User Interface Plugin 7.0
3 3
  * https://github.com/blueimp/jQuery-File-Upload
4 4
  *
5 5
  * Copyright 2010, Sebastian Tschan
@@ -139,12 +139,12 @@
139 139
             // Callback for successful uploads:
140 140
             done: function (e, data) {
141 141
                 var that = $(this).data('fileupload'),
  142
+                    files = that._getFilesFromResponse(data),
142 143
                     template;
143 144
                 if (data.context) {
144 145
                     data.context.each(function (index) {
145  
-                        var file = ($.isArray(data.result) &&
146  
-                                data.result[index]) ||
147  
-                                    {error: 'Empty file upload result'};
  146
+                        var file = files[index] ||
  147
+                                {error: 'Empty file upload result'};
148 148
                         if (file.error) {
149 149
                             that._adjustMaxNumberOfFiles(1);
150 150
                         }
@@ -164,8 +164,8 @@
164 164
                         );
165 165
                     });
166 166
                 } else {
167  
-                    if ($.isArray(data.result)) {
168  
-                        $.each(data.result, function (index, file) {
  167
+                    if (files.length) {
  168
+                        $.each(files, function (index, file) {
169 169
                             if (data.maxNumberOfFilesAdjusted && file.error) {
170 170
                                 that._adjustMaxNumberOfFiles(1);
171 171
                             } else if (!data.maxNumberOfFilesAdjusted &&
@@ -175,7 +175,7 @@
175 175
                         });
176 176
                         data.maxNumberOfFilesAdjusted = true;
177 177
                     }
178  
-                    template = that._renderDownload(data.result)
  178
+                    template = that._renderDownload(files)
179 179
                         .appendTo(that.options.filesContainer);
180 180
                     that._forceReflow(template);
181 181
                     that._transition(template).done(
@@ -307,6 +307,13 @@
307 307
             }
308 308
         },
309 309
 
  310
+        _getFilesFromResponse: function (data) {
  311
+            if (data.result && $.isArray(data.result.files)) {
  312
+                return data.result.files;
  313
+            }
  314
+            return [];
  315
+        },
  316
+
310 317
         // Link handler, that allows to download files
311 318
         // by drag & drop of the links to the desktop:
312 319
         _enableDragToDesktop: function () {
8  js/main.js
... ...
@@ -1,5 +1,5 @@
1 1
 /*
2  
- * jQuery File Upload Plugin JS Example 6.11
  2
+ * jQuery File Upload Plugin JS Example 7.0
3 3
  * https://github.com/blueimp/jQuery-File-Upload
4 4
  *
5 5
  * Copyright 2010, Sebastian Tschan
@@ -75,10 +75,8 @@ $(function () {
75 75
             dataType: 'json',
76 76
             context: $('#fileupload')[0]
77 77
         }).done(function (result) {
78  
-            if (result && result.length) {
79  
-                $(this).fileupload('option', 'done')
80  
-                    .call(this, null, {result: result});
81  
-            }
  78
+            $(this).fileupload('option', 'done')
  79
+                .call(this, null, {result: result});
82 80
         });
83 81
     }
84 82
 
6  server/gae-go/app/main.go
... ...
@@ -1,5 +1,5 @@
1 1
 /*
2  
- * jQuery File Upload Plugin GAE Go Example 2.1.3
  2
+ * jQuery File Upload Plugin GAE Go Example 3.0
3 3
  * https://github.com/blueimp/jQuery-File-Upload
4 4
  *
5 5
  * Copyright 2011, Sebastian Tschan
@@ -216,7 +216,9 @@ func get(w http.ResponseWriter, r *http.Request) {
216 216
 }
217 217
 
218 218
 func post(w http.ResponseWriter, r *http.Request) {
219  
-	b, err := json.Marshal(handleUploads(r))
  219
+    result := make(map[string][]*FileInfo, 1)
  220
+    result["files"] = handleUploads(r)
  221
+	b, err := json.Marshal(result)
220 222
 	check(err)
221 223
 	if redirect := r.FormValue("redirect"); redirect != "" {
222 224
 	    if strings.Contains(redirect, "%s") {
5  server/gae-python/main.py
... ...
@@ -1,6 +1,6 @@
1 1
 # -*- coding: utf-8 -*-
2 2
 #
3  
-# jQuery File Upload Plugin GAE Python Example 1.1.5
  3
+# jQuery File Upload Plugin GAE Python Example 2.0
4 4
 # https://github.com/blueimp/jQuery-File-Upload
5 5
 #
6 6
 # Copyright 2011, Sebastian Tschan
@@ -117,7 +117,8 @@ def get(self):
117 117
     def post(self):
118 118
         if (self.request.get('_method') == 'DELETE'):
119 119
             return self.delete()
120  
-        s = json.dumps(self.handle_upload(), separators=(',',':'))
  120
+        result = {'files': self.handle_upload()}
  121
+        s = json.dumps(result, separators=(',',':'))
121 122
         redirect = self.request.get('redirect')
122 123
         if redirect:
123 124
             return self.redirect(str(
10  server/node/server.js
... ...
@@ -1,6 +1,6 @@
1 1
 #!/usr/bin/env node
2 2
 /*
3  
- * jQuery File Upload Plugin Node.js Example 1.0.4
  3
+ * jQuery File Upload Plugin Node.js Example 2.0
4 4
  * https://github.com/blueimp/jQuery-File-Upload
5 5
  *
6 6
  * Copyright 2012, Sebastian Tschan
@@ -200,7 +200,7 @@
200 200
                     files.push(fileInfo);
201 201
                 }
202 202
             });
203  
-            handler.callback(files);
  203
+            handler.callback({files: files});
204 204
         });
205 205
     };
206 206
     UploadHandler.prototype.post = function () {
@@ -217,7 +217,7 @@
217 217
                     files.forEach(function (fileInfo) {
218 218
                         fileInfo.initUrls(handler.req);
219 219
                     });
220  
-                    handler.callback(files, redirect);
  220
+                    handler.callback({files: files}, redirect);
221 221
                 }
222 222
             };
223 223
         form.uploadDir = options.tmpDir;
@@ -273,10 +273,10 @@
273 273
                 Object.keys(options.imageVersions).forEach(function (version) {
274 274
                     fs.unlink(options.uploadDir + '/' + version + '/' + fileName);
275 275
                 });
276  
-                handler.callback(!ex);
  276
+                handler.callback({success: !ex});
277 277
             });
278 278
         } else {
279  
-            handler.callback(false);
  279
+            handler.callback({success: false});
280 280
         }
281 281
     };
282 282
     if (options.ssl) {
18  server/php/UploadHandler.php
... ...
@@ -1,6 +1,6 @@
1 1
 <?php
2 2
 /*
3  
- * jQuery File Upload Plugin PHP Class 5.19.2
  3
+ * jQuery File Upload Plugin PHP Class 6.0
4 4
  * https://github.com/blueimp/jQuery-File-Upload
5 5
  *
6 6
  * Copyright 2010, Sebastian Tschan
@@ -657,11 +657,11 @@ public function get($print_response = true) {
657 657
         }
658 658
         $file_name = $this->get_file_name_param();
659 659
         if ($file_name) {
660  
-            $info = $this->get_file_object($file_name);
  660
+            $response = array('file' => $this->get_file_object($file_name));
661 661
         } else {
662  
-            $info = $this->get_file_objects();
  662
+            $response = array('files' => $this->get_file_objects());
663 663
         }
664  
-        return $this->generate_response($info, $print_response);
  664
+        return $this->generate_response($response, $print_response);
665 665
     }
666 666
 
667 667
     public function post($print_response = true) {
@@ -682,12 +682,12 @@ public function post($print_response = true) {
682 682
         $content_range = isset($_SERVER['HTTP_CONTENT_RANGE']) ?
683 683
             preg_split('/[^0-9]+/', $_SERVER['HTTP_CONTENT_RANGE']) : null;
684 684
         $size =  $content_range ? $content_range[3] : null;
685  
-        $info = array();
  685
+        $files = array();
686 686
         if ($upload && is_array($upload['tmp_name'])) {
687 687
             // param_name is an array identifier like "files[]",
688 688
             // $_FILES is a multi-dimensional array:
689 689
             foreach ($upload['tmp_name'] as $index => $value) {
690  
-                $info[] = $this->handle_file_upload(
  690
+                $files[] = $this->handle_file_upload(
691 691
                     $upload['tmp_name'][$index],
692 692
                     $file_name ? $file_name : $upload['name'][$index],
693 693
                     $size ? $size : $upload['size'][$index],
@@ -700,7 +700,7 @@ public function post($print_response = true) {
700 700
         } else {
701 701
             // param_name is a single object identifier like "file",
702 702
             // $_FILES is a one-dimensional array:
703  
-            $info[] = $this->handle_file_upload(
  703
+            $files[] = $this->handle_file_upload(
704 704
                 isset($upload['tmp_name']) ? $upload['tmp_name'] : null,
705 705
                 $file_name ? $file_name : (isset($upload['name']) ?
706 706
                         $upload['name'] : null),
@@ -713,7 +713,7 @@ public function post($print_response = true) {
713 713
                 $content_range
714 714
             );
715 715
         }
716  
-        return $this->generate_response($info, $print_response);
  716
+        return $this->generate_response(array('files' => $files), $print_response);
717 717
     }
718 718
 
719 719
     public function delete($print_response = true) {
@@ -730,7 +730,7 @@ public function delete($print_response = true) {
730 730
                 }
731 731
             }
732 732
         }
733  
-        return $this->generate_response($success, $print_response);
  733
+        return $this->generate_response(array('success' => $success), $print_response);
734 734
     }
735 735
 
736 736
 }
6  test/test.js
... ...
@@ -1,5 +1,5 @@
1 1
 /*
2  
- * jQuery File Upload Plugin Test 6.11
  2
+ * jQuery File Upload Plugin Test 7.0
3 3
  * https://github.com/blueimp/jQuery-File-Upload
4 4
  *
5 5
  * Copyright 2010, Sebastian Tschan
@@ -18,8 +18,8 @@ $(function () {
18 18
     QUnit.done = function () {
19 19
         // Delete all uploaded files:
20 20
         var url = $('#fileupload').prop('action');
21  
-        $.getJSON(url, function (files) {
22  
-            $.each(files, function (index, file) {
  21
+        $.getJSON(url, function (result) {
  22
+            $.each(result.files, function (index, file) {
23 23
                 $.ajax({
24 24
                     url: url + '?file=' + encodeURIComponent(file.name),
25 25
                     type: 'DELETE'

0 notes on commit fb455d8

Please sign in to comment.
Something went wrong with that request. Please try again.