Opened 2 years ago

Closed 2 years ago

#20948 closed Uncategorized (invalid)

Form ImageField, MemoryFileUploadHandler and reading content of the InMemoryUploadedFile

Reported by: p_hrechyshkin@… Owned by:
Component: Python 2 Version: 1.4
Severity: Normal Keywords: file upload
Cc: p_hrechyshkin@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


There is a form with ImageField, which returns validation error

"Upload a valid image. The file you uploaded was either not an image or a corrupted image."

In my view I am trying to read image file content:

image = self.request.FILES.get('image')
if image:

content = "".join(image.chunks())

Validation error doesn't occur if I comment line with image.chunks(), also it isn't showed if I remove "django.core.files.uploadhandler.MemoryFileUploadHandler" from FILE_UPLOAD_HANDLERS setting.

So the reason could be in file object created by MemoryFileUploadHandler (InMemoryUploadedFile).

My guess was it could be that after read there is no call which set file reader pointer to the begging of the file.

It could be ImageFile.to_python assumes that pointer set to the beginning.
In ImageField.to_python I have added


file = StringIO(

and I haven't got image validation error after that.

Is it proper behavior? Or ImageField shouldn't assume that file haven't been read? Or InMemoryUploadedFile should call after chunks iterator ended its work?

Change History (4)

comment:1 Changed 2 years ago by apollo13

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

You are supposed to call yourself after you read from it. That said, it would be intresting to know why it works for TemporaryUploadedFile

comment:2 Changed 2 years ago by sduveen

  • Owner changed from nobody to sduveen
  • Status changed from new to assigned

comment:3 Changed 2 years ago by sduveen

  • Owner sduveen deleted
  • Status changed from assigned to new

comment:4 Changed 2 years ago by timo

  • Resolution set to invalid
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.
Back to Top