以下代码段将文件发送到浏览器。
# Prepare selected file for download...
filename = request.form.get('filename')
filepath = '/home/nikos/wsgi/static/files/'
return send_from_directory( filepath, filename )
如果我想在发送文件之前打印一些行怎么办?
# Prepare selected file for download...
pdata = pdata + '''Your file will be ready for download'''
padata = pdata + '''it will just atake a moment'''
filename = request.form.get('filename')
filepath = '/home/nikos/wsgi/static/files/'
return send_from_directory( filepath, filename )
如果我尝试添加pdata +响应,如:
return pdata + send_from_directory( filepath, filename )
我得到一个错误,返回应该只返回一个字符串,而不是字符串+响应
答案 0 :(得分:2)
如果您要同时返回一个字符串和一个响应对象,请尝试以下操作:
return (pdata, send_from_directory(filepath, filename))
这将返回一个包含两个数据类型的元组。除非您有两个兼容的对象,否则不能使用+
符号,例如"string" + "string"
或(tuple,) + (tuple,)
答案 1 :(得分:-1)
为什么不只在返回之前打印?
def add(a, b):
print(f"{a} is being added...")
print(f"{b} is being added..")
return a + b
c = add(1, 2)
print(c)
(xenial)vash@localhost:~/python/stack_overflow$ python3.7 strings.py 1 is being added... 2 is being added.. 3
建议:
print(f'{pdata} "Your file will be ready for download..."')
print(f'{pdata} "it will just atake a moment..."')
filename = request.form.get('filename')
filepath = '/home/nikos/wsgi/static/files/'
return send_from_directory( filepath, filename )
旧版Python:
print(f'{} "Your file will be ready for download..."'.format(pdata))
print(f'{} "it will just atake a moment..."'.format(pdata))
filename = request.form.get('filename')
filepath = '/home/nikos/wsgi/static/files/'
return send_from_directory( filepath, filename )