为什么我的文件上传(到Flask服务器)没有出现在request.files中但出现在request.stream中?

Ben*_*Ben 4 javascript forms iframe file-upload flask

我有一个表单(恰好在iframe中),如下所示:

<form id='picture-file-input-button' action='/post-picture' method='post' enctype='multipart/form-data' encoding='multipart/form-data'>
    <button type='button' class='choose-picture-button'>Choose picture</button>
    <input class='picture-file-input' type='file' />
</form>
Run Code Online (Sandbox Code Playgroud)

我使用$('#picture-file-input-button').submit()提交表单,这样可以正常工作.请求被发送到正确的URL并且我的Flask函数将其拾取并且响应被正确地发送回客户端(iframe被重新加载).

我没有在Flask做任何花哨的事情.它看起来像这样:

@app.route('/post-picture', methods=['POST'])
def post_provider_page_route():
    app.logger.debug(request)
    app.logger.debug(request.data)
    app.logger.debug(request.stream)
    app.logger.debug(request.files)
    app.logger.debug(request.form)
    return render_template('iframe_template.html')
Run Code Online (Sandbox Code Playgroud)

调试所有输出为空的ImmutableMultiDict,但request.stream除外,它输出:

<werkzeug.wsgi.LimitedStream object at 0x9a7d30c>
Run Code Online (Sandbox Code Playgroud)

我希望看到该文件显示在request.files中.为什么它不会出现在request.files中?

Ben*_*Ben 7

除非文件输入元素设置了name属性,否则不会填充request.files.name属性可以设置为任何内容.所以上面代码的修复是改变:

<input class='picture-file-input' type='file' />
Run Code Online (Sandbox Code Playgroud)

<input class='picture-file-input' type='file' name='picture' />
Run Code Online (Sandbox Code Playgroud)