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?

Community
  • 1
  • 1
  • 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? – Selcuk Feb 22 '16 at 04: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? – someguyinafloppyhat Feb 22 '16 at 20:37
  • You should read the documentation on [FileField](https://docs.djangoproject.com/es/1.9/ref/models/fields/#filefield) that clearly explains the storage mechanisms. Binary chunks are predefined bytes of data, which are imho easier to conceptualize than text lines. – Selcuk Feb 23 '16 at 01: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. – someguyinafloppyhat Feb 23 '16 at 14:05

0 Answers0