From af23baec6d8df09e7027fed8805e59e92c40fd6b Mon Sep 17 00:00:00 2001 From: nazoko <58116139+abczi@users.noreply.github.com> Date: Mon, 15 Jun 2026 19:48:11 +0800 Subject: [PATCH] fix(wechat): fix voice upload file handle and Content-Type issues (#5951) --- app/modules/wechat/wechat.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/app/modules/wechat/wechat.py b/app/modules/wechat/wechat.py index 368396ec..de884086 100644 --- a/app/modules/wechat/wechat.py +++ b/app/modules/wechat/wechat.py @@ -419,18 +419,23 @@ class WeChat: media_type=media_type, ) try: + # Read file content into memory to avoid file handle issues with media_path.open("rb") as media_file: - response = RequestUtils(timeout=60).request( - method="post", - url=req_url, - files={ - "media": ( - media_path.name, - media_file, - "voice/amr" if media_type == "voice" else "application/octet-stream", - ) - }, - ) + file_content = media_file.read() + + # Use RequestUtils with Accept header to avoid Content-Type conflicts + response = RequestUtils(timeout=60).request( + method="post", + url=req_url, + headers={"Accept": "application/json"}, + files={ + "media": ( + media_path.name, + file_content, + "voice/amr" if media_type == "voice" else "application/octet-stream", + ) + }, + ) except Exception as err: logger.error(f"上传企业微信临时素材失败:{err}") return None