如何实现request.files中的文件不为空

时间:2018-07-06 19:38:54

标签: python flask flask-wtforms

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

bindPreferenceSummaryToValue(findPreference(Preferences.DIR_SP_FILTERS));

下面的代码是上面的代码段,但是我认为我会为上下文添加整个功能。我的预期功能是:在遍历每个FileField时,检索每个FileField的数据,然后运行检查以查看是否在每个字段中上传了文件。如果有数据,则将该文件上传到S3 Bucket。如果FileField为空,则循环继续到下一个FileField。我尝试了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>' if len(request.files[file]) != 0if not request.files.get(file, None)都无济于事,因为它们要么导致400错误请求,要么即使上传了文件也忽略了每个FileField,或者继续上传空白文件。

if not request.files[file].filename == ''

1 个答案:

答案 0 :(得分:0)

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