【问题标题】:Flask request.files.getlist() appearing to be empty on post requestFlask request.files.getlist() 在发布请求时显示为空
【发布时间】:2022-01-30 09:35:16
【问题描述】:

我正在尝试使用烧瓶上传多个文件。但是在提交表单后我得到了空列表。 这是我的 HTML 表单代码:

<form enctype="multipart/form-data" action="/upload" method="post" role="form">
              <div class="form-row">
                <div class="col-lg-6 form-group">
                  <input type="text" name="name" class="form-control" id="name" placeholder="Your Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" required />
                  <div class="validate"></div>
                </div>
                <div class="col-lg-6 form-group">
                  <input type="email" class="form-control" name="email" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid email" required />
                  <div class="validate"></div>
                </div>
              </div>
              <div class="form-group">
                <input type="tel" class="form-control" name="phone" id="subject" placeholder="Phone Number" data-rule="minlen:4" data-msg="Please enter your phone number" required />
                <div class="validate"></div>
              </div>
              <div class="form-group">
                <select name="cata" class="browser-default custom-select">
                  <option selected>Culture, Arts and Social Scineces</option>
                  <option value="1">Medicine and Health</option>
                  <option value="2">Leadership, Managment, Buisness and Commerce</option>
                  <option value="3">Sceince, Agriculture and Engineering</option>
                  <option value="3">Other</option>
                </select>
              </div>
              <div class="form-group">
                <textarea class="form-control-a" name="message" rows="5" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea>
                <div class="validate"></div>
              </div>
              <div class="mb-3">
                <!-- <div class="loading">Loading</div>
                <div class="error-message"></div>
                <div class="sent-message">Your message has been sent. Thank you!</div> -->
              </div>
                <strong>Upload File:
                   <input type="file" name="atta_file[]" multiple>
                </strong>
                <input type="submit" style="border-radius: 5px;
                                            width: 110px;
                                            margin: 14px;
                                            height: 50px;
                                            line-height: 0;" 
                        class="btn btn-primary pull-right" 
                        value="Submit">
            </form>

这是我上传文件的 Python 脚本:

@app.route("/upload", methods=['POST','GET'])
def upload():
    if request.method == 'POST':
        filess = request.files.getlist('atta_file[]')
        print(filess)
        return render_template("upload.html")
    return render_template("upload.html")

最初我试图在控制台上打印提交的文件名,看看它是否工作。 我也为相同的查询寻找其他答案,但我发现在他们的问题中,他们只缺少 enctype="multipart/form-data"name 字段中的 输入标签。但我已经检查了我的代码,它们都没有丢失。我不确定我缺少什么以及如何解决此问题。

【问题讨论】:

  • 当我运行你的代码时,我会得到所有文件的列表。您使用不同的upload.html 的唯一想法 - 您应该在 Web 浏览器中检查 HTML 源代码 (Ctrl+U)
  • 顺便说一句:它不是 PHP,你不需要 [] in atta_file

标签: python html flask


【解决方案1】:

我不知道您是否还需要这方面的帮助。 但是我遇到了同样的问题,我想我找到了解决方案。 好吧,就我而言,我正在使用 ajax 请求来发送文件。

我意识到,当我在用于发送文件的 var 中建立索引时,在 python 中我只得到一个文件,然后我找到了这个 example

所以以这个例子作为参考。

JavaScript 代码:

var form_data = new FormData();
var auxnames = "";
var wordsF = $("#word").prop("files");
$.each(wordsF, function(i, file){
    form_data.append(file.name, file);
    auxnames += file.name+',';
});
form_data.append('auxindex', auxnames);
$.ajax({
    url: $SCRIPT_ROOT + "/upload_files",
    type:"post",
    data:form_data,
    contentType: false,
    cache: false,
    processData: false,
}).done(function(response){ ...

Python 代码:

if request.method == 'POST':
    auxindex = request.form['auxindex']
    auxindex = auxindex.split(",")
    auxindex = list(filter(None, auxindex))
    for file in auxindex:
        auxfile = request.files[file]
        namefile = secure_filename(file)
        auxfile.save(os.path.join(path, namefile))

也许不是最好的方法,但它对我有用,我希望这对你有用。

【讨论】:

    【解决方案2】:

    我遇到了类似的问题。我已经看到它推荐使用 request.files.getlist("文件[]") 或者 request.files.getlist("file")

    for file in request.files:
        files[file].save(out_filename)
    

    【讨论】:

      猜你喜欢
      • 2014-07-29
      • 2017-03-04
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 2018-07-09
      • 2018-11-18
      • 1970-01-01
      相关资源
      最近更新 更多