How to get the files uploaded in InMemoryUploadedFile django
Asked Answered
E

2

8

I am developing an application in django 2.1 in which I must upload an undetermined number of audios through a modal and then pass the information to the view from which the modal is launched. However, these audios should not be stored in the database until the main view form is filled out. Then I thought about these solutions:

First I thought about saving it as a session attribute but the contents of a FileField is not JSON serializable which did not work.

Second I thought about the LocalStorage property but if the files exceed the size I will have problems.

Third I thought about getting the file path and then create the audio but as I was reading is a bad practice and can only be obtained if the file is created on the disk, that is, if it is in TemporaryUploadedFile but my files should weigh less than one 1MB

For which I have the option that all files that are loaded with a size smaller than 2.5MB are stored in InMemoryUploadedFile but I do not know how to obtain them. Does anyone know how this is done? or how else can I save a list of temporary audios?

Exothermic answered 15/3, 2019 at 22:31 Comment(0)
H
13

InMemoryUploadedFile is a wrapper around a file object. You can access the file object using the file attribute.

file_in_memory # <InMemoryUploadedFile: xxx (xxx/xxx)>
file_object = file_in_memory.file
Highpressure answered 20/3, 2019 at 18:5 Comment(0)
H
1

ClientSide :

        var file = document.getElementById('file');
        const formData = new FormData();
        formData.append('file', file[0]);

        fetch('api/upload_file/',{
            method:'POST',
            body:formData
            
        }).then(res => console.log(res);
        .catch(error => console.log(error);

On Server Side:
InMemoryUploadedFile can be accessed as:

file = request.FILES.get('file')
Hf answered 15/4, 2021 at 14:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.