Opened 5 years ago

Last modified 4 years ago

#17955 new Bug

Uploading a file without using django forms

Reported by: alexandre@… Owned by: nobody
Component: HTTP handling Version: 1.3
Severity: Normal Keywords: HttpRequest, MultiPartParser
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no



i was trying to upload a file to my django backend without using django forms, but i was not able to it : my file was constantly getting dropped by the multiparser!

my multipart/formdata post request has two standard string post values :

Content-Disposition: form-data; name="foo" \r\n\r\n

and one file :
Content-Disposition: form-data; filename="bar"\r\n
Content-Type: image/png\r\n\r\n

I made it work simply by changing the file this way :

Current :

92	    def parse(self):
141	                try:
142	                    disposition = meta_data['content-disposition'][1]
143	                    field_name = disposition['name'].strip()
144	                except (KeyError, IndexError, AttributeError):
145	                    continue

New :

    def parse(self):
            field_name = ''
                disposition = meta_data['content-disposition'][1]
                if disposition.has_key('name'):
                    field_name = disposition['name'].strip()
                    field_name = disposition['filename'].strip()
            except (KeyError, IndexError, AttributeError):

I think the change is pretty straightforward, and would be really nice!

Change History (6)

comment:1 Changed 5 years ago by anonymous

Resolution: invalid
Status: newclosed

comment:2 Changed 5 years ago by Claude Paroz

Resolution: invalid
Status: closedreopened

Tickets shouldn't be anonymously closed without any explanation.

comment:3 Changed 5 years ago by Preston Holmes

for reference, this seems to be pointing to section 4.4 of

However, looking at the parser and your description, it is not clear how you are forming your post data exactly.

Is the file data being included encoded into the form data, or as a file?

It seems to me that the issue should be resolved in parse_boundary_stream, so that the TYPE = FILE is set up properly, as then on line 167, the 'filename' is retrieved correctly

What would be ideal is if you could create a tests patch - perhaps in regressiontests/file_uploads/ that demonstrates the failure?

comment:4 Changed 5 years ago by alexandre@…

The thing is, I was missing the "name" field of the content-disposition. It worked smoothly after I added it.

Although I agree it seems there is an issue with section 4.4

What i also think could be nice, is having a warning/error telling you your POST data were not well formatted, as django drops the data. (Took me some time to notice, since my code was working with other sites, ie facebook)

comment:5 Changed 4 years ago by Luke Plant

Triage Stage: UnreviewedAccepted
Type: Cleanup/optimizationBug

comment:6 Changed 4 years ago by Aymeric Augustin

Status: reopenednew
Note: See TracTickets for help on using tickets.
Back to Top