当前位置 博文首页 > 小龙狗的博客:flask生成Word文档并写入内存返回前端
问题描述
上代码
#!/usr/bin/python
# coding:utf-8
import json
from flask import Flask
from flask import make_response
import io
import docx
app = Flask(__name__)
@app.route('/downloadDoc' , methods=[ 'GET'])
def download():
newdocx = docx.Document()
file_stream = io.BytesIO()
newdocx.save(file_stream)
file_stream.seek(0)
response = make_response(file_stream.getvalue())
response.headers['content-type'] = 'application/octet-stream;charset=utf-8'
response.headers['content-disposition'] = 'attachment;filename=' + 'test.docx'
return response
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8765, debug=True)
测试
http://localhost:8765/downloadDocx
cs