I have a handler for a POST method in Django which receives an uploaded file. What I would like to do is verify that the file is a valid zip file before proceeding.
So, I have:
@login_required(login_url="login/")
def upload(request):
if request.method == 'POST' and request.FILES['upload_file']:
uploaded_file = request.FILES['upload_file']
print type(uploaded_file)
return render(request, 'upload.html', {'context': RequestContext(request)})
Now at this point uploaded_file
is of type <class 'django.core.files.uploadedfile.InMemoryUploadedFile'>
. My question is what would be the best way to verify that this is a valid archive? Do I need to save it to the disk and then use the zipfile
module or is there some way to do it without writing to the disk?
Note: I am not using the Django model with a FileField and the corresponding Form for various unrelated reasons.