12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import asyncio
- import json
- import os
- from datetime import time, datetime
- from flask import Flask, request, jsonify, send_from_directory, send_file
- app = Flask(__name__)
- cur_dir = os.getcwd()
- @app.route('/sysvent/draw/<int:template_id>', methods=['POST'])
- def post_json(template_id):
- # 检查请求是否包含JSON
- if not request.is_json:
- return jsonify({"error": "缺少JSON数据"}), 400
- # 从数据库中查找template
- # template = select_template_by_id(template_id) template = 'template/moban.dxf'
- data = request.json
- now = datetime.now()
- current_time_str = now.strftime("%Y_%m_%d_%H_%M_%S")
- json_file_name = f'{cur_dir}\\save\\{current_time_str}.json'
- dxf_file_name = f'{cur_dir}\\save\\{current_time_str}.DXF'
- with open(json_file_name, 'w', encoding='utf-8') as f:
- json.dump(data, f, ensure_ascii=False, indent=4)
- # asyncio.run(async_write_dxf_file(templateFile=template, node_list=data, save_name=dxf_file_name))
- res = {
- "code": 200,
- "msg": "success",
- 'data': {
- 'filename': f'{current_time_str}.DXF'
- }
- }
- return jsonify(res), 200
- @app.route('/sysvent/download/<filename>', methods=['GET'])
- def download_dxf_file(filename):
- # 指定文件的完整路径
- file_path = f'save\\{filename}'
- # 使用send_file发送文件,as_attachment=True表示以附件形式发送
- return send_file(file_path, as_attachment=True)
- if __name__ == '__main__':
- app.run(debug=True)
|