Code

Opened 2 years ago

Last modified 13 months 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

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!

Attachments (0)

Change History (6)

comment:1 Changed 2 years ago by anonymous

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

comment:2 Changed 2 years ago by claudep

  • Resolution invalid deleted
  • Status changed from closed to reopened

Tickets shouldn't be anonymously closed without any explanation.

comment:3 Changed 2 years ago by ptone

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 Changed 2 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 23 months ago by lukeplant

  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Cleanup/optimization to Bug

comment:6 Changed 13 months ago by aaugustin

  • Status changed from reopened to new

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as new
The owner will be changed from nobody to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.