I am trying to catch a file sent with form and perform some operations on it before it will be saved. So I need to create a copy of this file in temp directory, but I don\'t
As mentioned by @Sławomir Lenart, when uploading large files, you don't want to clog up system memory with a data.read().
From Django docs :
Looping over
UploadedFile.chunks()instead of usingread()ensures that large files don't overwhelm your system's memory
from django.core.files.storage import default_storage
filename = "whatever.xyz" # received file name
file_obj = request.data['file']
with default_storage.open('tmp/'+filename, 'wb+') as destination:
for chunk in file_obj.chunks():
destination.write(chunk)
This will save the file at MEDIA_ROOT/tmp/ as your default_storage will unless told otherwise.