Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Fix file upload using basic submission #119

Closed
wants to merge 1 commit into from

3 participants

Igor Sobreira Julian Bez Patrick Kranzlmueller
Igor Sobreira

The file upload wasn't working for me on IE, the issue was actually on the _upload_file() view.
With this patch it's working fine.

I also updated the tests for it (they were commented). Feedback is welcome.

Patrick Kranzlmueller sehmaschine closed this in 9473895 October 08, 2012
Julian Bez webjunkie commented on the diff February 13, 2013
filebrowser/sites.py
((22 lines not shown))
489 482
                     raise Http404('Invalid request! Multiple files included.')
490  
-                # filedata.name = convert_filename(upload.name)
491  
-                filedata.name = convert_filename(request.POST.get('file_name'))
  483
+                filedata = request.FILES.values()[0]
  484
+
  485
+            try:
  486
+                filedata.name = convert_filename(request.GET['qqfile'])
  487
+            except KeyError:
  488
+                return HttpResponseBadRequest('Invalid request! No filename given.')
1

I think you never import HttpResponseBadRequest!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Oct 03, 2012
Igor Sobreira fixed basic submission upload 2e7f9e8
This page is out of date. Refresh to see the latest.
25  filebrowser/sites.py
@@ -473,22 +473,19 @@ def _upload_file(self, request):
473 473
         Upload file to the server.
474 474
         """
475 475
         if request.method == "POST":
476  
-            if request.is_ajax(): # Advanced (AJAX) submission
477  
-                folder = request.GET.get('folder')
  476
+            folder = request.GET.get('folder', '')
  477
+
  478
+            if request.is_ajax():          # Advanced (AJAX) submission
478 479
                 filedata = ContentFile(request.raw_post_data)
479  
-                try:
480  
-                    filedata.name = convert_filename(request.GET['qqfile'])
481  
-                except KeyError:
482  
-                    return HttpResponseBadRequest('Invalid request! No filename given.')
483  
-            else: # Basic (iframe) submission
484  
-                # TODO: This needs some attention, do we use this at all?
485  
-                folder = request.POST.get('folder')
486  
-                if len(request.FILES) == 1:
487  
-                    filedata = request.FILES.values()[0]
488  
-                else:
  480
+            else:                          # Basic (iframe) submission
  481
+                if len(request.FILES) != 1:
489 482
                     raise Http404('Invalid request! Multiple files included.')
490  
-                # filedata.name = convert_filename(upload.name)
491  
-                filedata.name = convert_filename(request.POST.get('file_name'))
  483
+                filedata = request.FILES.values()[0]
  484
+
  485
+            try:
  486
+                filedata.name = convert_filename(request.GET['qqfile'])
  487
+            except KeyError:
  488
+                return HttpResponseBadRequest('Invalid request! No filename given.')
492 489
 
493 490
             fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("filebrowser:fb_upload", current_app=self.name))
494 491
             folder = fb_uploadurl_re.sub('', folder)
43  filebrowser/tests/sites.py
@@ -9,6 +9,7 @@
9 9
 """
10 10
 
11 11
 # PYTHON IMPORTS
  12
+from __future__ import with_statement
12 13
 import os
13 14
 import sys
14 15
 import shutil
@@ -89,12 +90,13 @@ def test_upload(test):
89 90
 
90 91
 def test_do_upload(test):
91 92
     ## Attemp an upload using AJAX SUBMISSION
92  
-    f = open(os.path.join(PATH_FILEBROWSER_MEDIA, 'img/testimage.jpg'), "rb")
93  
-    file_size = os.path.getsize(f.name)
  93
+
94 94
     url = reverse('%s:fb_do_upload' % test.site_name)
95 95
     url = '?'.join([url, urlencode({'folder': test.tmpdir.path_relative_directory, 'qqfile': 'testimage.jpg'})])
96  
-    response = test.c.post(url, data=f.read(), content_type='application/octet-stream', HTTP_X_REQUESTED_WITH='XMLHttpRequest', X_File_Name='testimage.jpg')
97  
-    f.close()
  96
+
  97
+    with open(os.path.join(PATH_FILEBROWSER_MEDIA, 'img/testimage.jpg'), "rb") as f:
  98
+        file_size = os.path.getsize(f.name)
  99
+        response = test.c.post(url, data=f.read(), content_type='application/octet-stream', HTTP_X_REQUESTED_WITH='XMLHttpRequest', X_File_Name='testimage.jpg')
98 100
     
99 101
     # Check we get OK response
100 102
     test.assertTrue(response.status_code == 200)
@@ -106,22 +108,25 @@ def test_do_upload(test):
106 108
     
107 109
     # Check the file has the correct size
108 110
     test.assertTrue(file_size == test.site.storage.size(path))
  111
+
  112
+    ## Attemp an upload of the file using BASIC SUBMISSION
  113
+
  114
+    url = reverse('%s:fb_do_upload' % test.site_name)
  115
+    url = '?'.join([url, urlencode({'folder': test.tmpdir.path_relative_directory, 'qqfile': 'testimage_basic.jpg'})])
  116
+
  117
+    with open(os.path.join(PATH_FILEBROWSER_MEDIA, 'img/testimage.jpg'), 'rb') as f:
  118
+        file_size = os.path.getsize(f.name)
  119
+        response = test.c.post(url, {'qqfile': f})
  120
+
  121
+    # Check we get OK response
  122
+    test.assertTrue(response.status_code == 200)
109 123
     
110  
-    # ## Attemp an upload of the file using BASIC SUBMISSION
111  
-    # f = open(os.path.join(PATH_FILEBROWSER_MEDIA, 'img/testimage.jpg'))
112  
-    # url = reverse('%s:fb_do_upload' % test.site_name)
113  
-    # response = test.c.post(url, {'file':f, 'folder': test.tmpdir.path_relative_directory, 'file_name': 'testimage_basic.jpg'})
114  
-    # f.close()
115  
-    
116  
-    # # Check we get OK response
117  
-    # test.assertTrue(response.status_code == 200)
118  
-    
119  
-    # # Check the file now exists
120  
-    # abs_path = os.path.join(test.tmpdir.path, 'testimage_basic.jpg')
121  
-    # test.assertTrue(test.site.storage.exists(abs_path))
122  
-    
123  
-    # # Check the file has the correct size
124  
-    # test.assertTrue(file_size == os.path.getsize(abs_path))
  124
+    # Check the file now exists
  125
+    path = os.path.join(test.tmpdir.path, 'testimage_basic.jpg')
  126
+    test.assertTrue(test.site.storage.exists(path))
  127
+
  128
+    # Check the file has the correct size
  129
+    test.assertTrue(file_size == test.site.storage.size(path))
125 130
 
126 131
 def test_detail(test):
127 132
     """
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.