1 存:遍历服务器中的所有文件存取
Postgresql中的字段filelist,类型为text
filepath= '/data/'+request.POST.get('filepath') # 获取到路径
filesize, fileCount = get_server_file_total_size(filepath) # 获取文件中的所有文件的大小
metafile = get_server_subfilename(filepath) # 遍历文件夹中的子文件(夹)和大小
metafile是字典类型
filelist = json.dumps(metafile) # 转成json数据进行存储
2 取:
planid = request.GET.get('planId')
sql = '''SELECT id, name, filepath, filesize, filelist,"fileCount"
FROM exp_system.exp_datametarelate
WHERE planid=%s;'''
cursor.execute(sql, [planid])
fileinfo = cursor.fetchone()
if fileinfo:
if fileinfo[4]:
filelist = json.loads(fileinfo[4]) # 还原json数据
else:
filelist = fileinfo[4]
3 显示
<div class="modal-body">
<table class="table table-condensed table-hover">
<thead>
<tr>
<th>文件名</th>
<th>文件大小</th>
</tr>
</thead>
<tbody>
{% for i in f.filelist %}
<tr>
<td>{{ i.filename }}</td>
<td>
{% if i.filesize > 1024 %}
{% if i.filesize > 1048576 %}
{% widthratio i.filesize 1048576 1 %} MB
{% else %}
{% widthratio i.filesize 1024 1 %} KB
{% endif %}
{% else %}
{{ i.filesize }} B
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>