0

There are dozens of posts on SO about Django, that describe how to use forms and models, or file streams, to upload files (e.g. images or text files). Two SO posts in particular represent good examples of this:

  1. Need a minimal Django file upload example
  2. how to save a file in django without creating model

The first example uses a model to store the file. The second example opens a file stream and writes the contents to disk, chunk by chunk.

What I want to do is upload a binary file (e.g. Excel spreadsheet) and save it to a local directory (i.e. not in a database), without using a model and without opening a file stream. Is this possible? Can you provide a minimal example?

I've read the Django docs concerning file uploads and storage, but I've only found examples that rely on models or file streams. Is it not possible to directly save an uploaded file to disk?

4
  • 1
    The first example does not store the file in a database. It saves the file to the local storage (in 'documents/%Y/%m/%d' folder) and simply stores the file name in the database. And what is wrong with a filestream? Commented Feb 22, 2016 at 4:01
  • Well then I misunderstood what was going on with the model based version. If only the name is stored in the DB, that certainly is not made clear in the Django docs. Regarding a filestream approach, I don't see how chunking works with a binary file; with a text file, I can see how lines of text can be separated into discrete parts. But what are the discrete units of a binary file? Commented Feb 22, 2016 at 20:37
  • You should read the documentation on FileField that clearly explains the storage mechanisms. Binary chunks are predefined bytes of data, which are imho easier to conceptualize than text lines. Commented Feb 23, 2016 at 1:31
  • Thank you. I'm self-learning Django, coming from a scientific programming background. Have not used databases before, so still grappling with basics. Also, while I commonly use filestreams in C++ programs, its only for ASCII formatted files (usually large data tables), not binary. Thank you for taking the time to give advice. Commented Feb 23, 2016 at 14:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.