How to copy InMemoryUploadedFile object to disk
This is similar question, it might help.
import os
from django.core.files.storage import default_storage
from django.core.files.base import ContentFile
from django.conf import settings
data = request.FILES['image'] # or self.files['image'] in your form
path = default_storage.save('tmp/somename.mp3', ContentFile(data.read()))
tmp_file = os.path.join(settings.MEDIA_ROOT, path)
Django: store InMemoryUploadedFile on the disk
You can override the default in FILE_UPLOAD_HANDLERS
in settings.py
# only use TemporaryFileUploadHandler for file uploads
FILE_UPLOAD_HANDLERS = (
'django.core.files.uploadhandler.TemporaryFileUploadHandler',
)
How to locally save a pdf file from file object?
Simple upload function:
def upload_func(file):
with open('your/custom/path/filename.fileformat', 'wb+') as f:
for chunk in file.chunks():
f.write(chunk)
Upload with a model:
model:
class MyFileModel:
file = models.FileField()
# ...
upload:
my_obj = MyFileModel.objects.create(file=request.FILES['file'])
You can use upload_to
to change the path:
FileField.upload_to
django file uploads - move or copy the file?
Yes, the same applies to Unix systems... where a "drive letter" is more of a path, so you might find that /opt/django/YourApp and /opt/django/tmpUploadDir are actually two different devices.
So, trusting on "move" it's not really worth it, by and large. On the other hand, you may do your copying outside the client viewing process, after he/she has dismissed the "100% complete" dialog.
Or you might factor the copy time in the total percentage. Say that you know that copy speed is 1 Mb per second, and you observe network upload speed proceeding at 100 Kb per second. Then you know that for a 1 GB file total transfer time will be 1100 seconds (not 1000), and once calculated that the "old" percentage is X, you will be able to display it as X*DiskSpeed/(DiskSpeed + NetworkSpeed). When the user has uploaded 100% of the file, he will see the progress bar at 91%, growing until it reaches 100% when copy is also complete.
This last method can be used at advantage if your processing is more than a simple copy, e.g. if you are recoding a video through ffmpeg. You do need a good guesstimate of the processing time beforehand, though, otherwise you'll end up with an "upload" bar that, while nonmonotonic (i.e. you should never see it going from 80% to 79%), will progress in fits and starts.
My opinion is that compared to upload time, copy time is negligible and may be safely ignored. If it is not, then maybe displaying a second, different progress bar might be in order, so that the user doesn't see anything he/she might believe "anomalous".
Related Topics
Why Do I Need to Deploy a "Default" App Before I Can Deploy Multiple Services in Gae
How to Create a Numpy Array of Arbitrary Length Strings
What Exactly Is the Point of Memoryview in Python
Pygame Tic Tak Toe Logic? How Would I Do It
Read Unicode Characters from Command-Line Arguments in Python 2.X on Windows
How to Assign a Variable in an If Condition, and Then Return It
Pandas Style Function to Highlight Specific Columns
Argparse with Required Subparser
Get Inserted Key Before Commit Session
Printing Tuple with String Formatting in Python
Convert Bytes to Floating Point Numbers