使用 Python 在 post 之前获取文件大小
Getting file size before post with Python
我的 Flask 服务器上有一个 API,我可以使用以下代码从客户端上传文件:
@app.route('/api/uploadJob', methods = ['GET', 'POST'])
def uolpadJob():
try:
if request.method == 'POST':
f = request.files['file']
fullFilePath = os.path.join(app.config['UPLOAD_FOLDER'],
secure_filename(f.filename))
#fileSize = ???
f.save(fullFilePath)
我想在将其保存到我的硬盘之前获取文件大小,以便我可以将其与可用磁盘进行比较space并选择是否要保存它或return 一条错误信息。
如何在实际上传之前获取文件大小?
如果您想在保存前检查尺寸详细信息,这可能会有所帮助:
@app.route('/api/uploadJob', methods = ['GET', 'POST'])
def uolpadJob():
try:
if request.method == 'POST':
f = request.files['file']
fullFilePath = os.path.join(app.config['UPLOAD_FOLDER'],
secure_filename(f.filename))
f.seek(0, 2)
file_length = f.tell()
# Introduce your disk space condition and save on basis of that
f.save(fullFilePath)
但是,如果您想在将文件保存到指定路径后进行检查,请尝试以下操作:
@app.route('/api/uploadJob', methods = ['GET', 'POST'])
def uolpadJob():
try:
if request.method == 'POST':
f = request.files['file']
fullFilePath = os.path.join(app.config['UPLOAD_FOLDER'],
secure_filename(f.filename))
f.save(fullFilePath)
fileSize = os.stat(fullFilePath).st_size
我的 Flask 服务器上有一个 API,我可以使用以下代码从客户端上传文件:
@app.route('/api/uploadJob', methods = ['GET', 'POST'])
def uolpadJob():
try:
if request.method == 'POST':
f = request.files['file']
fullFilePath = os.path.join(app.config['UPLOAD_FOLDER'],
secure_filename(f.filename))
#fileSize = ???
f.save(fullFilePath)
我想在将其保存到我的硬盘之前获取文件大小,以便我可以将其与可用磁盘进行比较space并选择是否要保存它或return 一条错误信息。 如何在实际上传之前获取文件大小?
如果您想在保存前检查尺寸详细信息,这可能会有所帮助:
@app.route('/api/uploadJob', methods = ['GET', 'POST'])
def uolpadJob():
try:
if request.method == 'POST':
f = request.files['file']
fullFilePath = os.path.join(app.config['UPLOAD_FOLDER'],
secure_filename(f.filename))
f.seek(0, 2)
file_length = f.tell()
# Introduce your disk space condition and save on basis of that
f.save(fullFilePath)
但是,如果您想在将文件保存到指定路径后进行检查,请尝试以下操作:
@app.route('/api/uploadJob', methods = ['GET', 'POST'])
def uolpadJob():
try:
if request.method == 'POST':
f = request.files['file']
fullFilePath = os.path.join(app.config['UPLOAD_FOLDER'],
secure_filename(f.filename))
f.save(fullFilePath)
fileSize = os.stat(fullFilePath).st_size