我在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)
我想先获取文件大小,然后再将其保存到硬盘中,以便可以将其与可用磁盘空间进行比较,然后选择要保存还是返回错误消息。在实际上传之前如何获取文件大小?
如果要在保存之前检查尺寸的详细信息,这可能会有所帮助:
@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