【发布时间】:2016-10-18 20:37:24
【问题描述】:
我这里有以下情况。我的操作系统显示我通过 POST 请求获得的 django TemporaryUploadedFile 不再存在,但不知何故可以读取这个上传的文件。
这是代码
text_file = request.FILES['text_file']
print(text_file.temporary_file_path())
os.system('ls -l ' + text_file.temporary_file_path())
fs = FileSystemStorage()
file_new =fs.save(text_file.name, text_file)
print(text_file.temporary_file_path())
os.system('ls -l ' + text_file.temporary_file_path())
fs.delete(file_new)
for chunk in text_file.chunks():
text += chunk.decode(encoding)
print('Got text OK.')
这给出了以下输出:
/tmp/tmp0tngal9t.upload foo.txt
-rw------- 1 mine machine 3072889 oct 18 19:29 /tmp/tmp0tngal9t.upload
/tmp/tmp0tngal9t.upload foo.txt
ls: cannot access '/tmp/tmp0tngal9t.upload': No such file or directory
Got text OK.
所以TemporaryUploadedFile 在保存到file_new 后消失了,后来也被删除了。无论如何text_file被块成功读取,我从上传的foo.txt文件中获取所有文本。怎么可能?如果text_file 不再存在,text_file.chunks() 从哪里获取数据?
我用:
python 3.5.2django 1.10.2ubuntu 16.04.1
【问题讨论】:
标签: python linux django file-upload temporary-files