我需要以csv格式发送db数据:
def send_csv():
mkdir_p('csv')
outfile = open('csv/output.csv', 'w')
outcsv = csv.writer(outfile)
cursor = g.db.execute('Select * from my_table')
outcsv.writerows(cursor)
outfile.close()
return send_file('csv/output.csv', attachment_filename='output.csv')
但是这会在send_file期间出现以下错误:
RuntimeError: Attempted implicit sequence conversion but the response object is in direct passthrough mode.
答案 0 :(得分:0)
问题是你应该将direct_passthrough设置为false; https://www.programcreek.com/python/example/58917/flask.send_file
您也可以在不写入fs的情况下发送文件; flask make_response with large files
答案 1 :(得分:0)
首先在您的脚本中导入以下行。
来自flask import send_from_directory 的
无论如何,您将csv保存在一个文件夹中,并使用名为 send_from_directory 的内置函数来提供该文件。 将您的return语句更改为此行,它将起作用。 返回send_from_directory('文件夹路径',file_name,as_attachment = True) 有关 send_from_directory 的详细信息,请查看以下链接。 https://www.programcreek.com/python/example/65747/flask.send_from_directory