Jaqueline Passos Jaqueline Passos - 1 year ago 95
Python Question

How to manipulate user uploaded files in django without saving it?

I'm making an app which gets data from a csv file and generates a graph using it. All files contains the same structure. I've decided I'm not going to store the files, because of server prices. I'm going to use heroku for now to host this app. It's a Django app. I'm wondering how I could only open the file and extract the data of it using django. I've thought about creating a model and save each file content on it. How can I do this? Is this the best option for my case?

Answer Source

I'll assume you're using Django 1.9 (if not, please update your question to reflect your version of Django).

First, using the HTML <form> tag, you'll want to include enctype="multipart/form-data" as an attribute.

Assuming you'll name your <input> tag as myFile (<form name="myFile">), in your view, simply extract the file from request.FILES:

def my_file_upload(request):
    if request.method == 'POST':
        myFile = request.FILES.get('myFile')
        # At this point, myFile is an instance of UploadedFile, see
        # https://docs.djangoproject.com/en/1.9/ref/files/uploads/ for details

Read up on uploaded files in the Django documentation to see the caveats associated with this approach.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download