0

How to store image file or file temporary using python or django rest_framework without saving in the database?

In dajngo rest_framework I did these things in a function based view.

temp_file = request.FILES['document_file']

Then, how I store this file temporary without saving in the database?

1 Answers1

0

You can refer the below function which handles a .txt file.You can create a temporary_folder directory. To write file to temporary_folder:

def handle_uploaded_file(f):
    with open('/full/path/to/temporary_folder/name.txt', 'wb+') as destination:
        for chunk in f.chunks():
            destination.write(chunk)

Once your business logic is done, you can delete the file. Please check this How to delete a file or folder?

Other Reference: https://docs.djangoproject.com/en/3.1/topics/http/file-uploads/

Amandeep Singh
  • 1,371
  • 1
  • 11
  • 33
  • After this, how can I use this file for further works. As a example to ectract data from the file and store them into a database using camelot, when this file is a PDF file. – anoja madusanka Feb 11 '21 at 03:21