Saturday, 27 July 2013

Save file from memory to disk in django

Get file from memory and save into temp directory of OS in django

This article basically for saving the file from memory to temporary directory in django.

Here is the sample code.

from import default_storage
from django.core.files.base import ContentFile
from django.conf import settings
def upload_excel_file(request):
    data = request.FILES['file']
    path ='tmp/' + str(data), ContentFile(
    tmp_file_path = os.path.join(settings.MEDIA_ROOT, path)

Now this tmp_file_path is the path of your file which is now saved into tmp directory of OS.

1 comment:

  1. Thanks i looked so many places for this, this was such a tremendous help. Thank You!