@@ -118,6 +118,10 @@ class FileUploadParser(BaseParser):
118
118
Parser for file upload data.
119
119
"""
120
120
media_type = '*/*'
121
+ errors = {
122
+ 'unhandled' : 'FileUpload parse error - none of upload handlers can handle the stream' ,
123
+ 'no_filename' : 'Missing filename. Request should include a Content-Disposition header with a filename parameter.' ,
124
+ }
121
125
122
126
def parse (self , stream , media_type = None , parser_context = None ):
123
127
"""
@@ -134,6 +138,9 @@ def parse(self, stream, media_type=None, parser_context=None):
134
138
upload_handlers = request .upload_handlers
135
139
filename = self .get_filename (stream , media_type , parser_context )
136
140
141
+ if not filename :
142
+ raise ParseError (self .errors ['no_filename' ])
143
+
137
144
# Note that this code is extracted from Django's handling of
138
145
# file uploads in MultiPartParser.
139
146
content_type = meta .get ('HTTP_CONTENT_TYPE' ,
@@ -146,7 +153,7 @@ def parse(self, stream, media_type=None, parser_context=None):
146
153
147
154
# See if the handler will want to take care of the parsing.
148
155
for handler in upload_handlers :
149
- result = handler .handle_raw_input (None ,
156
+ result = handler .handle_raw_input (stream ,
150
157
meta ,
151
158
content_length ,
152
159
None ,
@@ -178,10 +185,10 @@ def parse(self, stream, media_type=None, parser_context=None):
178
185
179
186
for index , handler in enumerate (upload_handlers ):
180
187
file_obj = handler .file_complete (counters [index ])
181
- if file_obj :
188
+ if file_obj is not None :
182
189
return DataAndFiles ({}, {'file' : file_obj })
183
- raise ParseError ( "FileUpload parse error - "
184
- "none of upload handlers can handle the stream" )
190
+
191
+ raise ParseError ( self . errors [ 'unhandled' ] )
185
192
186
193
def get_filename (self , stream , media_type , parser_context ):
187
194
"""
0 commit comments