我正在为我的烧瓶应用程序创建一个上传系统。当我上传一个zip文件时,我可以用request.data
在我的应用程序中访问它。但是,我不知道如何获取文件的大小。我试过request.data.read()
,但它不起作用。我得到以下错误:
'bytes' object has no attribute 'read'
我也试过request.data.tell()
、request.data.seek()
等
有什么建议吗?
解决问题的一个简单方法是:
- 如果您不想在设备上本地存储文件:
import os
file = request.files['file']
file.seek(0, os.SEEK_END)
file_length = file.tell()
# file_length should tell you the size of your .zip file
- 否则您可以使用:
request.files['file'].save('/tmp/file')
file_length = os.stat('/tmp/file').st_size
受到这个答案的启发:(
您可以使用jquery获取文件大小,然后在发送图像时发送文件大小。
$('#myFile').bind('change', function() {
//this.files[0].size gets the size of your file.
alert(this.files[0].size);
});
如果你想使用python知道大小,那么你可以使用os模块,但你需要先保存文件。
os.path.getsize("/path/isa_005.mp3")