Opened 13 years ago

Closed 4 years ago

Last modified 4 years ago

#17955 closed Bug (invalid)

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

Description

Hi,

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 multipartparser.py 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 = ''
            try:
                disposition = meta_data['content-disposition'][1]
                if disposition.has_key('name'):
                    field_name = disposition['name'].strip()
                else:
                    field_name = disposition['filename'].strip()
            except (KeyError, IndexError, AttributeError):
                continue

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

Change History (8)

comment:1 by anonymous, 13 years ago

Resolution: invalid
Status: newclosed

comment:2 by Claude Paroz, 13 years ago

Resolution: invalid
Status: closedreopened

Tickets shouldn't be anonymously closed without any explanation.

comment:3 by Preston Holmes, 13 years ago

for reference, this seems to be pointing to section 4.4 of http://www.ietf.org/rfc/rfc2388.txt

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/tests.py that demonstrates the failure?

comment:4 by alexandre@…, 13 years ago

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 by Luke Plant, 12 years ago

Triage Stage: UnreviewedAccepted
Type: Cleanup/optimizationBug

comment:6 by Aymeric Augustin, 12 years ago

Status: reopenednew

comment:7 by Mariusz Felisiak, 4 years ago

Resolution: invalid
Status: newclosed

According to the Section 4.2: "Content-Disposition Header Field for Each Part" in RFC 7578:

The Content-Disposition header field MUST also contain an additional parameter of "name";

so data without the name parameter are invalid. It's not an issue in Django but in provided data. I think it's time to close this issue.

comment:8 by Carlton Gibson, 4 years ago

Good spot, yes. The behaviour for request.FILES was clarified in 9e4b1ad33e436e8fe60af756d7e09639ee886ac2

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