烧瓶request.files为空 [英] Flask request.files is empty

查看:65
本文介绍了烧瓶request.files为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很像这个问题,我正在尝试遵循简单的Flask教程,用于将文件上传到Flask服务器.在我的特定情况下,我正在尝试上传XML文件.

much like this question, I'm trying to follow the simple Flask tutorial for file upload to a flask server. In my specific case, I'm trying to upload an XML file.

我正在使用的(简体)HTML是:

The (simplified) HTML I'm using is:

<form action="" method="post" enctype="multipart/form-data">
    <input type="file">
    <input type="submit" value="Let's go!">
</form>

请求由 if request.method =='POST':块正确处理,因此我输入了一些打印语句来进行故障排除:

The request is correctly handled by a if request.method == 'POST': block, so I put in some print statements to troubleshoot:

print('request.method', request.method)
print('request.args', request.args)
print('request.form', request.form)
print('request.files', request.files)

结果如下:

request.method POST
request.args ImmutableMultiDict([])
request.form ImmutableMultiDict([])
request.files ImmutableMultiDict([])

我做错了什么?如果需要,我可以提供更完整的源代码.

What am I doing wrong? I can provide more complete source code if needed.

推荐答案

和往常一样,我在发布此问题后仅几分钟就找到了答案.我在这里回答是希望能帮助其他人.

As always, I found the answer mere minutes after posting this question. I'm answering here to hopefully help someone else.

问题是我的文件输入没有 name 属性.感谢Ben在这里我能够通过在文件中添加 name 属性来解决此问题输入,现在文件上传已得到正确处理.

The problem was that my file input had no name attribute. Thanks to Ben here I was able to fix this problem by adding a name attribute to the file input, and now the file upload is being processed correctly.

这篇关于烧瓶request.files为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆