Flask报错:TypeError: downloadanddelete()获取到参数‘file’的多个值
Flask中TypeError: got multiple values for argument 'file'问题排查
问题描述
实现Flask文件下载并删除功能时,调用接口始终触发以下错误:
File "services.py", line 75, in decorated
return f(user, *args, **kwargs)
TypeError: downloadanddelete() got multiple values for argument 'file'
网上现有案例多为类函数或位置参数覆盖场景,无法匹配当前问题,需要排查思路或测试建议。
原始代码
services.py(主文件)
from downlaod import downlaod from functools import wraps from flask import * from flask_sqlalchemy import SQLAlchemy from apscheduler.schedulers.background import BackgroundScheduler # create app app = Flask(__name__) # config for app --> link database app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://////opt/services/databases/users.db' db = SQLAlchemy(app) User = database(db=db) def auth(f): @wraps(f) def decorated(*args, **kwargs): #request authorization and save data in auth auth = request.authorization # if auth or username or password are empty if not auth or not auth.username or not auth.password: return make_response('Could not verify', 401, {'WWW-Authenticate' : 'Basic realm="Login required!"'}) # find the user user = User.query.filter_by(username=auth.username).first() # if user is empty if not user: return make_response('Could not verify', 401, {'WWW-Authenticate' : 'Basic realm="Login required!"'}) # if password is not correct if user.password != auth.password: return make_response('Could not verify', 401, {'WWW-Authenticate' : 'Basic realm="Login required!"'}) # if password is correct if user.password == auth.password: # gib user zurück return f(user, *args, **kwargs) return decorated @app.route('/download/<file>', methods=['GET']) @auth def downloadanddelete(file,current_app): r = downlaoad(file=file,current_app=current_app) return r if __name__ == '__main__': app.run(port=8081, host="127.0.0.1")
downloadanddelete.py
# import Lib from flask import make_response, Flask, send_file import os def downlaod(file,current_app): # path of temp path = '/opt/services/temp/' + file # generate a file in the ram than delete the file from SSD or HDD def generate(): with open(path, "rb") as f: yield from f os.remove(path) r = current_app.response_class(generate()) r.headers.set('Content-Disposition', 'attachment', file) #send file return r
本地测试与修改
本地复现原始代码时,触发的错误为:TypeError: downloadanddelete() missing 1 required positional argument: 'current_app'
通过直接从Flask导入current_app而非作为参数传递,修改后本地代码可正常运行:
services.py修改部分
@app.route('/download/<file>', methods=['GET']) def downloadanddelete(file): return downlaod(file=file)
downloadanddelete.py修改部分
from flask import make_response, Flask, current_app import os def downlaod(file): path = 'C:\\Users\\my_username\\temp' def generate(): with open(path, "rb") as f: yield from f os.remove(path) r = current_app.response_class(generate()) r.headers.set('Content-Disposition', 'attachment', filename=file) return r
服务器端问题
将上述本地可行的修改部署到服务器后,仍触发最初的错误:
File "services.py", line 75, in decorated return f(user, *args, **kwargs)
TypeError: downloadanddelete() got multiple values for argument 'file'
正在尝试本地复现该错误,寻求排查思路。
排查思路建议
- 核对服务器与本地代码一致性:确认服务器端
services.py的路由定义、视图函数参数、auth装饰器逻辑完全同步本地修改,避免存在代码差异 - 调整视图函数参数接收方式:
auth装饰器会将user作为位置参数传入视图函数,而Flask路由会将<file>作为关键字参数传递,若视图函数未正确接收user参数,会导致参数冲突。修改视图函数如下:@app.route('/download/<file>', methods=['GET']) @auth def downloadanddelete(user, file): return downlaod(file=file) - 检查Flask版本差异:执行
pip show flask对比本地与服务器的Flask版本,部分版本对路由参数传递、装饰器的处理逻辑存在差异 - 添加日志调试:在
auth装饰器和downloadanddelete函数中添加打印语句,输出args和kwargs的具体内容,明确参数传递情况:# 在auth装饰器的decorated函数中添加 print(f"args: {args}, kwargs: {kwargs}") # 在downloadanddelete函数中添加 print(f"User: {user}, File: {file}") - 验证路由规则唯一性:检查服务器端是否存在重复定义的
/download/<file>路由,避免路由冲突导致参数传递异常
内容的提问来源于stack exchange,提问作者AlexR
相关产品推荐
相关产品推荐

