有没有办法用Flask接收多个上传的文件?我尝试了以下方法:
<form method="POST" enctype="multipart/form-data" action="/upload"> <input type="file" name="file[]" multiple=""> <input type="submit" value="add"> </form>
然后打印内容request.files['file']:
request.files['file']:
@app.route('/upload', methods=['POST']) def upload(): if not _upload_dir: raise ValueError('Uploads are disabled.') uploaded_file = flask.request.files['file'] print uploaded_file media.add_for_upload(uploaded_file, _upload_dir) return flask.redirect(flask.url_for('_main'))
如果我上传多个文件,它将仅打印该集中的第一个文件:
<FileStorage: u'test_file.mp3' ('audio/mp3')>
是否可以使用Flask的内置上传处理方式接收多个文件?谢谢你的帮助!
你可以使用flask.request.files的方法getlist,例如:
@app.route("/upload", methods=["POST"]) def upload(): uploaded_files = flask.request.files.getlist("file[]") print uploaded_files return ""