On Mon, Aug 22, 2016 at 10:11:49AM -0400, Larry Martell wrote:
> When I get the request, request.FILES is empty. Yet the content type
> is multipart/form-data and the method is POST:
>
> (Pdb) print request.META['CONTENT_TYPE']
> multipart/form-data;
> boundary="boundary_.oOo._NzEwNjIzMTM4MTI4NjUxOTM5OQ==MTY2NjE4MDk5Nw=="
>
> (Pdb) print request.META['REQUEST_METHOD']
> POST
>
> (Pdb) print request.FILES
> <MultiValueDict: {}>
>
> The individual files are being sent with content-type
> 'application/octet-stream', and it looks like I get the contents of
> the files converted to unicode:
>
> (Pdb) type(request.POST['right-carotidartery:63B2E474-D690-445F-B92A-31EBADDC9D93.png'])
> <type 'unicode'>
Are you certain the request contains correct headers for files? In
particular, look at the “Content-Disposition” header; for each file,
it should contain the “filename” attribute; otherwise, Django will
treat it as a regular (non-file) form field [1]. As an example, the
header could look like this::
Content-Disposition: form-data; name="file"; filename="filename.png"
> I've tried to decoding it with:
>
> unicodedata.normalize('NFKD', request.POST[key]).encode('ascii','ignore'))
>
> But that did not create a valid PNG file. It seems that something
> mistakenly encoded to unicode. What would be doing that?
By this point, it's too late – Django has already treated the field as
a string field, so it's already called
``force_text(data, encoding, errors='replace')`` [2], which means the
value you have there has already lost some information that is
impossible to get back [3].
And even if it was reversible, performing a Unicode normalization, and
then throwing away everything non-ASCII would most certainly not be
the correct way, since it would mangle about one half of the input
data into ASCII.
> Any suggestions on how I can make this work?
You'll have to make sure that the request is correct, I'm afraid.
Good luck,
Michal
[1]:
https://github.com/django/django/blob/a8f957797d8035a542cdb20b03aaebd81b9529e2/django/http/multipartparser.py#L638-L641
[2]:
https://github.com/django/django/blob/a8f957797d8035a542cdb20b03aaebd81b9529e2/django/http/multipartparser.py#L207
[3]:
https://docs.python.org/2/library/codecs.html#codec-base-classes