Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python and Django - How to use in memory and temporary files

I need some examples with file operations using in memory and temporary files.

I searched a lot for a good example /tutorial and I found just basic read/write/append operations.

I need to understand how can I read in Django a file that is uploaded(an image) before the save(post) is finished.

Because Django is Python, I think is better to understand first in python.

I checked the Django documentation/examples about, but it is not very clear so I need to understand first in Python then in Django how the operations are working, not just copy and paste.

I know how to use ImageFields, upload default operation, I'm interested only in using "in memory and temporary files."

I want to use this in combination with a crop function. So the user can upload 1, 2, 3... images, and with a javascript crop script I get the coordinates in a hidden field. After javascript simulated crop I show the user a thumbnail with the crop, how will look like ratio

The user can change his mind and can edit/update or delete a file before saving.

Now depending on the file size it can be keep in memory or write.

like image 668
user3541631 Avatar asked Aug 24 '17 16:08

user3541631


People also ask

How to save file in storage in Django?

- Create a NamedTemporaryFile instead of TemporaryFile as Django's ImageField requires file name. - Iterate over your in-memory file and write blocks of data to the NamedTemporaryFile object. - Flush the file to ensure the file is written to the storage. - Change the temporary file to a Django's File object.

How do I find the path of an uploaded file in Python?

Method 2: Find the path to the given file using os. In order to obtain the Current Working Directory in Python, use the os. getcwd() method. This function of the Python OS module returns the string containing the absolute path to the current working directory.

What is SimpleUploadedFile?

class SimpleUploadedFile(InMemoryUploadedFile): """ A simple representation of a file, which just has content, size, and a name. """ def __init__(self, name, content, content_type="text/plain"): content = content or b"" super().

How do you create a file like an object in Python?

To create a file object in Python use the built-in functions, such as open() and os. popen() . IOError exception is raised when a file object is misused, or file operation fails for an I/O-related reason. For example, when you try to write to a file when a file is opened in read-only mode.


1 Answers

When a file is uploaded Django will do one of two things: store it in memory if the file is small (< 2 MB last time I checked), or store it as a temporary file on disk if it's large. This behavior is configurable via the FILE_UPLOAD_HANDLERS setting. So, your web server and Django take care of the actual upload and storage, but it's your job to process the file before the request is over, otherwise the file is deleted.

Uploaded files are accessible through the request.FILES property. Each key in FILES will match the name of the file input on your <form>. The value is an UploadedFile object, which is a stream you can use to read the file data.

For example, say you have an <input name="img" type="file" /> and you want to detect if the image is completely white. You don't need to store the file for this, you just need to load it into memory, process it to get the result and then let it be discarded.

from PIL import Image

def some_view(request):
    if request.method == 'POST':
        img_file = request.FILES['img']

        if img_file.size > 2000000:
            return HttpResponseBadRequest()

        img = Image.open(img_file)

        # analyze the image...

Another possibility is that someone is uploading a backup file that is quite large (lets say 2 GB), and you need to store it somewhere. It's effectively the same thing, except we read the file into memory in chunks, then write each chunk to disk somewhere else so that it's saved after the request finishes.

def some_view(request):
    if request.method == 'POST':
        backup_file = request.FILES['backup_file']
        with open('some/file/name.bak', 'wb+') as destination:
            for chunk in backup_file.chunks():
                destination.write(chunk)

        # file is saved

When the request is over, the uploaded file is stored at some/file/name.bak.

Whether it's in memory or a temporary file is usually not important because the interface is the same. You can read a temporary file just like you can read an in memory file.

like image 54
mindcruzer Avatar answered Sep 19 '22 01:09

mindcruzer