How to zip multiple uploaded file in Django before saving it to database?

Viewed 751

I am trying to compress a folder before saving it to database/file storage system using Django. For this task I am using ZipFile library. Here is the code of view.py:

class BasicUploadView(View):
    def get(self, request):
        file_list = file_information.objects.all()
        return render(self.request, 'fileupload_app/basic_upload/index.html',{'files':file_list})

    def post(self, request):
        zipfile = ZipFile('test.zip','w')
        if request.method == "POST":
            for upload_file in request.FILES.getlist('file'): ## index.html name
                zipfile.write(io.BytesIO(upload_file))
                fs = FileSystemStorage()
                content = fs.save(upload_file.name,upload_file)
                data = {'name':fs.get_available_name(content), 'url':fs.url(content)}
            zipfile.close()
        return JsonResponse(data)

But I am getting the following error:

TypeError: a bytes-like object is required, not 'InMemoryUploadedFile'

Is there any solution for this problem? Since I may have to upload folder with large files, do I have to write a custom TemporaryFileUploadHandler for this purpose? I have recently started working with Django and it is quite new to me. Please help me with some advice.

1 Answers
Related