I have a request that is uploading two files at once. The FILES attribute of the request
object looks like this:
<MultiValueDict: {u'output': [<InMemoryUploadedFile: output.txt (text/plain)>], u'history': [<InMemoryUploadedFile: .history.json (application/octet-stream)>]}>
I store both files as variables:
output = request.FILES['output']
history = request.FILES['history']
I then assign two file fields on a Django model to these files to try and save them but only the file field that is assigned to output
is saved properly, the other field that is supposed to contain the contents of history contains no content.
After trying to solve this issue in the debugger I found that I am able to read the contents of history
only once, after which history.read()
returns an empty string: ''
. output
can be read as many times as I would like and it still returns the outputs
's actual content.
I have tried a bunch of different hacky strategies to save the content of history
, including creating new files using File
and ContentFile
however it appears that django is defying any of my attempts to create a file that returns this content on .read()
For the record, history
contains alot of binary-looking data that is in valid JSON format and is about 12k in size. file
says that it is:
ASCII text, with very long lines, with no line terminators
Is there something I am missing with the way Django is handling history
?
EDIT: Here are the fields on the model I am trying to save (though I believe the issue is occurring before saving):
history_file = models.FileField(
upload_to=history_file_name,
null=True,
blank=True
)
output_file = models.FileField(
upload_to=output_file_name,
null=True,
blank=True
)
Here is the code were the model is being saved:
result.output_file = output
challenge.history_file = history
challenge.save()
This issue is occuring in the post method of a CBV that is based on View. I have not changed any of the default file handling settings that come with Django 1.6.5