None.js 第五步 Buffer(缓冲区)

本文详细介绍了Node.js中Buffer对象的使用方法,包括如何创建Buffer实例、填充数据、转换为字符串以及序列化与反序列化的操作。通过示例代码展示了Buffer在实际应用中的常见用法。

// 存数据,取数据;
buf = Buffer.alloc(26);
for (var i = 0; i < 26; i++) {
    buf[i] = i + 97;
}
  • console.log(buf.toString('ascii'));
  • console.log(buf.toString('ascii',0,5));
  • console.log(buf.toString('utf8',0,5));
  • console.log(buf.toString(undefined, 0, 5));
输出
    abcdefghijklmnopqrstuvwxyz
    abcde
    abcde
    abcde
const buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5]);
const json = JSON.stringify(buf);

// 输出: {"type":"Buffer","data":[1,2,3,4,5]}
console.log(json);

const copy = JSON.parse(json, (key, value) => {
    //  这个地方最初的时候没有理解,其实原因很简单,就是运算符的优先级导致的
    //  这个时候如果单独打印
    value && value.type === 'Buffer'
    // 返回的其实是一个 false 并不是我们理解的一个true
    // 这个时候输出的是正确的
    return value && value.type === 'Buffer' ?
        Buffer.from(value.data) :
        value;

    // 这个时候输出的竟然是 lin
    return value && value.type === 'Buffer' ?
        "lin" :   // lin
        value;

    // 这个时候输出的就是 m 
    return value && value.type === 'Buffer' ?
        "lin" :
        "m";   // m

    // 这个时候输出的也是 m 
    return value && value.type === 'Buffer' ?
        Buffer.from(value.data) :
        "m";   // m
});

// 输出: <Buffer 01 02 03 04 05>
console.log(copy);

转载于:https://www.cnblogs.com/mcat/p/8438855.html

@app.route(‘/export_weekly/’) def export_weekly(emp_id): 获取查询参数 from_date = request.args.get(‘from’, default=‘2025-08-25’) to_date = request.args.get(‘to’, default=‘2025-08-31’) try: # 获取用户数据 user_data = json.loads(merged_df_json) # 查找匹配的用户 user = next((u for u in user_data if u[‘工号’] == emp_id | u[‘employee_id’] == emp_id), None) if not user: return jsonify({“error”: “用户不存在”}), 404 # 获取任务数据 tasks = json.loads(merged_df_json) # 过滤出当前用户且在日期范围内的任务 tasks = [ t for t in tasks if t[‘emp_id’] == emp_id and from_date <= t[‘date’] <= to_date ] # 3. 生成周报文档 doc_buffer = generate_weekly_report_docx( user[‘name’], from_date, to_date, tasks ) # 4. 创建下载文件名 filename = f"{user[‘name’]}周报至.docx" # 5. 返回文件响应 return send_file( doc_buffer, as_attachment=True, download_name=filename, mimetype=‘application/vnd.openxmlformats-officedocument.wordprocessingml.document’ ) except Exception as e: return jsonify({“error”: f"生成周报失败: {str(e)}"}), 500 def generate_weekly_report_docx(username, start_date, end_date, tasks): “”“从JSON数据生成周报DOCX文档”“” doc = Document() 添加标题 doc.add_heading(f"{username}的周报", 0) doc.add_paragraph(f"时间范围:{start_date} ~ {end_date}“) doc.add_paragraph() # 创建表格 if tasks: table = doc.add_table(rows=1, cols=3) table.style = ‘Table Grid’ # 添加表格边框 # 设置表头 header = table.rows[0].cells header[0].text = ‘日期’ header[1].text = ‘任务内容’ header[2].text = ‘状态’ # 填充任务数据 for task in tasks: row_cells = table.add_row().cells row_cells[0].text = task[‘date’] row_cells[1].text = task[‘description’] row_cells[2].text = task[‘status’] else: doc.add_paragraph(“本周无任务记录”, style=‘Intense Quote’) # 添加页脚 doc.add_paragraph() doc.add_paragraph(f"生成时间: {datetime.now().strftime(‘%Y-%m-%d %H:%M:%S’)}”, style=‘Caption’) # 保存到内存缓冲区 buffer = BytesIO() doc.save(buffer) buffer.seek(0) return buffer把周报显示在前端
最新发布
09-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值