# Uploading many files with GenericView / Adapted FileUploadHandler

**URL:** https://forum.djangoproject.com/t/uploading-many-files-with-genericview-adapted-fileuploadhandler/17151
**Category:** Using Django
**Created:** [November 20, 2022, 11:45am UTC](https://forum.djangoproject.com/t/uploading-many-files-with-genericview-adapted-fileuploadhandler/17151 "2022-11-20T11:45:21Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![jdede](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jdede/32/10949_2.png) [@jdede](https://forum.djangoproject.com/u/jdede)
#### Post date: [November 20, 2022, 11:45am UTC](https://forum.djangoproject.com/t/uploading-many-files-with-genericview-adapted-fileuploadhandler/17151/1 "2022-11-20T11:45:21Z")

</div>

Dear all,  
I am currently writing an application where I upload many (i.e. 1000+) small images via a generic form (CreateView). With a small amount of files, it works without any problems. With an increasing number, I get the “Too many open files error”. Changing the limit of simultaneously opened files on OS level is not an option. So I adapted the UploadHandler as follows to closed the files after the upload:

```auto
class ClosingFileTemporaryFileUploadHandler(TemporaryFileUploadHandler):
    def file_complete(self, *args, **kwargs):
        f = super().file_complete(*args, **kwargs)
        f.close()
        return f

```

Now, I am facing the problem that the form validation fails “Upload a valid image. The file you uploaded was either not an image or a corrupted image.”. I think I need adapt the validators in a way they first re-open the files, do the validation and close it again. The question is: How and where to do this optimally, i.e. in an iterative way over all received files? Or is there a more generic approach? I have the feeling that I am overcomplicating the thing.

I am using Django version 4.1.3, Python 3.10.8, Debian

Thanks a lot for your support.  
Best regards  
Jens
