Django: How to save InMemoryUploadedFile (a temporary uploaded file) to disk?

When a user uploads a file using forms.FileField, you usually want to save it in some way.

To stash the file away on disk, just use the following snippet.

from django.core.files.storage import default_storage
from django.core.files.base import ContentFile

file = request.FILES['your_file_fieldname']
path = default_storage.save('heart_of_the_swarm.txt', ContentFile(file.read()))

There you have it!

i4IaIek6mz5sW

Now back to more important things, like eating cheerleaders.

Source

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog