【问题标题】:How to implement if file in request.files is not empty如果request.files中的文件不为空如何实现
【发布时间】:2018-07-06 19:38:53
【问题描述】:

我试图实现的逻辑:如果用户没有将文件上传到 Filefield 从而将其留空,则不要将文件上传到 S3 存储桶或创建指向它的链接。第 8/9 行没有达到我的预期,因为它仍然将空文件上传到存储桶并在我的票证上创建指向它的链接。

uploads = [['frontview', 'upload', '<b>Frontview: </b>'],['backview', 'upload2', '<b>Backview: </b>'], ['electricalsupply', 'upload3', '<b>Electrical Supply: </b>'], ['fullstructure', 'upload4', '<b>Full Structure: </b>'], ['controlroom', 'upload5', '<b>Control Room: </b>'], ['otherdoc', 'upload6', '<b>Other Documentation: </b>'], ['customersignoff', 'upload7', '<b>Customer Sign-Off: </b>']]
        file_upload_urls = []
        folder = str(datetime.datetime.now()).replace(' ', '') + '/' # sets folder to the current date & time while getting rid of spaces & adding forward slash for linking purposes
        for upload in uploads: #loops through uploads array
            files = request.files.getlist(upload[1])
            section_urls = []
            for file in files: #loops through uploads array more specifically each File (upload, upload2, upload3, etc)
                if file not in request.files:
                    filename = str(upload[0] + '_' + file.filename).replace(' ', '')
                    section_urls.append(current_app.config['NEVCODOCS_BASE_URL'] + folder + filename)
                    path = "ServiceRegistration/" + folder + filename
                    s3.Bucket('nevcodocs').put_object(Key=path, Body=file)
            file_upload_urls.append([upload[2], section_urls])
​
        # Installation photos are placed in an unordered list & formats links of photos for ticket submission
        for section in file_upload_urls:
            ticket_html = ticket_html + section[0] + '<ul>'
            for url in section[1]:
                ticket_html = ticket_html + '<li> <href=' + url + '">' + url + '</href></li>'
            ticket_html = ticket_html + '</ul>'

下面的代码是上面的 sn-p,但我想我会为上下文添加整个函数。我的预期功能是:在遍历每个 FileField 时,检索每个 FileField 的数据,并检查是否在每个字段中上传了文件。如果有数据,则将该文件上传到 S3 存储桶。如果 FileField 为空,则循环移动到下一个 FileField。我试过 if len(request.files[file]) != 0if not request.files.get(file, None)if not request.files[file].filename == '' 都无济于事,因为它们要么导致 400 错误请求,要么即使上传了文件也会忽略每个 FileField,或者继续上传空白文件。

        for file in files: #loops through uploads array more specifically each FileField (upload, upload2, upload3, etc)
            # if not len(request.files[file]) == 0:
            if file not in files:
                filename = str(upload[0] + '_' + file.filename).replace(' ', '')
                section_urls.append(current_app.config['NEVCODOCS_BASE_URL'] + folder + filename)
                path = "ServiceRegistration/" + folder + filename
                s3.Bucket('nevcodocs').put_object(Key=path, Body=file)

【问题讨论】:

  • 请将所有相关代码粘贴到问题中。我们无法从屏幕截图中复制。
  • 很抱歉。认为它在粘贴箱中看起来会更干净。我已经用代码 sn-p 更新了帖子
  • 现在,请适当格式化,以便在 Python 中实际执行。
  • @StevenNguyen “看起来更干净”对于 Stack Overflow 来说不如永久可用更重要。如果 dpaste 出现问题或清除了他们的旧数据等,那么这个问题就不再是未来观众的有用参考。
  • 如果你循环遍历for file in files:if file not in files: 将永远是False,因为你刚刚从同一个列表中取出了文件。

标签: python flask flask-wtforms


【解决方案1】:

解决方案:如果不是 file.filename == '':

【讨论】:

    猜你喜欢
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-07
    • 2013-07-16
    • 1970-01-01
    相关资源
    最近更新 更多