diff --git a/django/http/multipartparser.py b/django/http/multipartparser.py
index 9413a1e..edf98f6 100644
a
|
b
|
class MultiPartParser(object):
|
199 | 199 | for chunk in field_stream: |
200 | 200 | if transfer_encoding == 'base64': |
201 | 201 | # We only special-case base64 transfer encoding |
| 202 | # We should always read base64 streams by multiple of 4 |
| 203 | over_bytes = len(chunk) % 4 |
| 204 | if over_bytes: |
| 205 | over_chunk = field_stream.read(4 - over_bytes) |
| 206 | chunk += over_chunk |
| 207 | |
202 | 208 | try: |
203 | 209 | chunk = base64.b64decode(chunk) |
204 | 210 | except Exception as e: |
diff --git a/tests/regressiontests/file_uploads/tests.py b/tests/regressiontests/file_uploads/tests.py
index 8fa140b..124ba5d 100644
a
|
b
|
class FileUploadTests(TestCase):
|
74 | 74 | |
75 | 75 | self.assertEqual(response.status_code, 200) |
76 | 76 | |
77 | | def test_base64_upload(self): |
78 | | test_string = "This data will be transmitted base64-encoded." |
| 77 | def _test_base64_upload(self,content): |
79 | 78 | payload = client.FakePayload("\r\n".join([ |
80 | 79 | '--' + client.BOUNDARY, |
81 | 80 | 'Content-Disposition: form-data; name="file"; filename="test.txt"', |
82 | 81 | 'Content-Type: application/octet-stream', |
83 | 82 | 'Content-Transfer-Encoding: base64', |
84 | 83 | '',])) |
85 | | payload.write(b"\r\n" + base64.b64encode(force_bytes(test_string)) + b"\r\n") |
| 84 | payload.write(b"\r\n" + base64.b64encode(force_bytes(content)) + b"\r\n") |
86 | 85 | payload.write('--' + client.BOUNDARY + '--\r\n') |
87 | 86 | r = { |
88 | 87 | 'CONTENT_LENGTH': len(payload), |
… |
… |
class FileUploadTests(TestCase):
|
94 | 93 | response = self.client.request(**r) |
95 | 94 | received = json.loads(response.content.decode('utf-8')) |
96 | 95 | |
97 | | self.assertEqual(received['file'], test_string) |
| 96 | self.assertEqual(received['file'], content) |
| 97 | |
| 98 | def test_base64_upload(self): |
| 99 | self._test_base64_upload("This data will be transmitted base64-encoded.") |
| 100 | |
| 101 | def test_big_base64_upload(self): |
| 102 | self._test_base64_upload("Big data" * 68000) # > 512Kb |
98 | 103 | |
99 | 104 | def test_unicode_file_name(self): |
100 | 105 | tdir = sys_tempfile.mkdtemp() |