#20020 closed Bug (invalid)
ValueError for FileField if the file is closed in clean method
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | File uploads/storage | Version: | 1.4 |
Severity: | Normal | Keywords: | FileField ValueError |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Hi,
Say you have a Model with a FileField, and you need to override clean to do some validation on the file before it is saved. If the file is closed within clean and the file is over 2.5MB (or larger than FILE_UPLOAD_MAX_MEMORY_SIZE) a "ValueError: I/O operation on closed file" will result. If you attempt to re-open the file in clean to avoid this error a different ValueError will result stating "The file cannot be reopened." This is particularly a problem if the verification of a file uses some other library that may open and close the file.
I'm using Django 1.4.3, and have seen the issue under the Django web development server as well as under Apache w/mod_wsgi. Here's an example models.py to illustrate the issue:
from string import join from django.db import models from FileUploadExample.settings import MEDIA_ROOT def some_file_operation(file): file.open() file.close() def upload_file_path(instance, filename): upload_dir = join([MEDIA_ROOT, str(filename)], '') return upload_dir class UploadedFile(models.Model): uploaded_file = models.FileField(upload_to=upload_file_path) def clean(self): # some call to somewhere to validate the file some_file_operation(self.uploaded_file)
Change History (5)
comment:1 by , 12 years ago
Keywords: | FileField ValueError added |
---|---|
Type: | Uncategorized → Bug |
comment:2 by , 12 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:3 by , 12 years ago
I'm not quite following, if it is deleted how can the file be re-opened?
This is the issue I am seeing, once the file is closed there is no way to proceed to save the model instance.
comment:4 by , 12 years ago
Sorry I wasn't clear -- you can re-open it by name as long as you haven't closed the original.
comment:5 by , 12 years ago
Got it, this works. I just create another file handle to give to a routine that closes files. Thanks
TemporaryUploadedFile
relies on Python's tempfile.NamedTemporaryFile to make sure the temporary file will be eventually deleted; in fact, it's deleted as soon as it's closed.That's why you cannot reopen it, and that isn't going to change — unless you come up with another way to guarantee that the temporary file gets deleted eventually.
At least on Unix it seems that the file can be re-opened by name. Use
self.uploaded_file.temporary_file_path
.