[Django] #17955: Uploading a file without using django forms

15 views
Skip to first unread message

Django

unread,
Mar 22, 2012, 10:05:11 PM3/22/12
to django-...@googlegroups.com
#17955: Uploading a file without using django forms
-------------------------------------+-------------------------------------
Reporter: alexandre@… | Owner: nobody
Type: | Status: new
Cleanup/optimization | Version: 1.3
Component: HTTP handling | Keywords: HttpRequest,
Severity: Normal | MultiPartParser
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
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!

--
Ticket URL: <https://code.djangoproject.com/ticket/17955>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 23, 2012, 1:46:29 PM3/23/12
to django-...@googlegroups.com
#17955: Uploading a file without using django forms
-------------------------------------+-------------------------------------
Reporter: alexandre@… | Owner: nobody
Type: | Status: closed
Cleanup/optimization | Version: 1.3
Component: HTTP handling | Resolution: invalid
Severity: Normal | Triage Stage:
Keywords: HttpRequest, | Unreviewed
MultiPartParser | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by anonymous):

* status: new => closed
* needs_better_patch: => 0
* resolution: => invalid
* needs_tests: => 0
* needs_docs: => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/17955#comment:1>

Django

unread,
Mar 23, 2012, 3:48:32 PM3/23/12
to django-...@googlegroups.com
#17955: Uploading a file without using django forms
-------------------------------------+-------------------------------------
Reporter: alexandre@… | Owner: nobody
Type: | Status: reopened
Cleanup/optimization | Version: 1.3
Component: HTTP handling | Resolution:
Severity: Normal | Triage Stage:
Keywords: HttpRequest, | Unreviewed
MultiPartParser | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by claudep):

* status: closed => reopened
* resolution: invalid =>


Comment:

Tickets shouldn't be anonymously closed without any explanation.

--
Ticket URL: <https://code.djangoproject.com/ticket/17955#comment:2>

Django

unread,
Mar 28, 2012, 11:59:17 AM3/28/12
to django-...@googlegroups.com
#17955: Uploading a file without using django forms
-------------------------------------+-------------------------------------
Reporter: alexandre@… | Owner: nobody
Type: | Status: reopened
Cleanup/optimization | Version: 1.3
Component: HTTP handling | Resolution:
Severity: Normal | Triage Stage:
Keywords: HttpRequest, | Unreviewed
MultiPartParser | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (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?

--
Ticket URL: <https://code.djangoproject.com/ticket/17955#comment:3>

Django

unread,
Mar 28, 2012, 1:04:22 PM3/28/12
to django-...@googlegroups.com
#17955: Uploading a file without using django forms
-------------------------------------+-------------------------------------
Reporter: alexandre@… | Owner: nobody
Type: | Status: reopened
Cleanup/optimization | Version: 1.3
Component: HTTP handling | Resolution:
Severity: Normal | Triage Stage:
Keywords: HttpRequest, | Unreviewed
MultiPartParser | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (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)

--
Ticket URL: <https://code.djangoproject.com/ticket/17955#comment:4>

Django

unread,
Jun 8, 2012, 10:16:00 PM6/8/12
to django-...@googlegroups.com
#17955: Uploading a file without using django forms
-------------------------------------+-------------------------------------
Reporter: alexandre@… | Owner: nobody
Type: Bug | Status: reopened
Component: HTTP handling | Version: 1.3
Severity: Normal | Resolution:
Keywords: HttpRequest, | Triage Stage: Accepted
MultiPartParser | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by lukeplant):

* type: Cleanup/optimization => Bug
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/17955#comment:5>

Django

unread,
Sep 24, 2020, 8:22:51 AM9/24/20
to django-...@googlegroups.com
#17955: Uploading a file without using django forms
-------------------------------------+-------------------------------------
Reporter: alexandre@… | Owner: nobody
Type: Bug | Status: closed

Component: HTTP handling | Version: 1.3
Severity: Normal | Resolution: invalid

Keywords: HttpRequest, | Triage Stage: Accepted
MultiPartParser |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => closed

* resolution: => invalid


Comment:

According to the [https://tools.ietf.org/html/rfc7578#section-4.2 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.

--
Ticket URL: <https://code.djangoproject.com/ticket/17955#comment:7>

Django

unread,
Sep 24, 2020, 8:49:21 AM9/24/20
to django-...@googlegroups.com
#17955: Uploading a file without using django forms
-------------------------------------+-------------------------------------
Reporter: alexandre@… | Owner: nobody
Type: Bug | Status: closed
Component: HTTP handling | Version: 1.3
Severity: Normal | Resolution: invalid
Keywords: HttpRequest, | Triage Stage: Accepted
MultiPartParser |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Carlton Gibson):

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

--
Ticket URL: <https://code.djangoproject.com/ticket/17955#comment:8>

Reply all
Reply to author
Forward
0 new messages