我正在尝试使用uppy.io's xhr和aiohttp后端构建上传表单。因此,我尝试使用烧瓶将uppy的示例迁移到aiohttp: https://github.com/transloadit/uppy/tree/master/examples/python-xhr
我尝试了aiohttp手册的文件上传部分中提到的请求处理程序和多部分阅读器: https://aiohttp.readthedocs.io/en/stable/web_quickstart.html#file-uploads
工作瓶代码(来自上面的示例):
@GetMapping({"/user", "/me"})
public Map<String, Object> user(Principal principal){
Map<String, Object> map = new LinkedHashMap<>();
map.put("name", principal.getName());
if( principal instanceof OAuth2Authentication) {
OAuth2Authentication oauth = (OAuth2Authentication)principal;
map.put("authorities",oauth.getUserAuthentication().getAuthorities()
.stream()
.map(GrantedAuthority::getAuthority)
.collect(Collectors.toList()));
}
return map;
}
我无法使用的aiohttp服务器:
@app.route('/upload', methods=['POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
print(request.files)
print(request.form['email'])
if len(request.files) == 0:
return jsonify(
error="No file n request"
), 400
for fi in request.files:
file = request.files[fi]
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return jsonify(
message="ok"
), 201
当我当前上传多个文件时,我的函数被多次执行。我感觉我使用错误的方法来处理XHR请求吗? aiohttp甚至能够处理XMLHttpRequests吗?