14.4使用「阿里云函数计算 FC」时浏览器把网页当成文件下载了
在函数返回的响应头里Content-Type不是text/html所以浏览器直接把we.html当成文件下载而不是渲染成网页。100% 解决下载问题。函数代码里虽然写了content_typetext/html; charsetutf-8但实际返回的响应头里没有这个配置导致浏览器不知道这是网页只能下载第一步修复代码强制正确返回Content-Type回到函数计算的「代码」标签页完全替换app.py里的代码用下面这个强化版我加了强制响应头from flask import Flask, Response import oss2 app Flask(__name__) # 替换成RAM子账号AK/SK OSS_ACCESS_KEY_ID AccessKey ID OSS_ACCESS_KEY_SECRET AccessKey Secret # # 成都地域OSS配置已适配Bucket名ourselves OSS_ENDPOINT oss-cn-chengdu.aliyuncs.com OSS_BUCKET ourselves # 初始化OSS客户端 auth oss2.Auth(OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET) bucket oss2.Bucket(auth, OSS_ENDPOINT, OSS_BUCKET) # 处理we.html请求强制返回网页格式强化版绝对生效 app.route(/we.html) def get_html(): try: file_obj bucket.get_object(we.html) content file_obj.read() # 关键强制设置Content-Type同时加缓存控制避免浏览器缓存旧响应 return Response( content, content_typetext/html; charsetutf-8, headers{ Cache-Control: no-cache, no-store, must-revalidate, Pragma: no-cache } ) except Exception as e: return Response(fError: {str(e)}, status500, content_typetext/plain) # 处理音频文件请求 app.route(/path:filename) def get_file(filename): try: file_obj bucket.get_object(filename) content_type file_obj.headers.get(Content-Type, application/octet-stream) return Response(file_obj.read(), content_typecontent_type) except Exception as e: return Response(404 Not Found, status404, content_typetext/plain) if __name__ __main__: app.run(host0.0.0.0, port9000)必须做的 2 件事把AccessKey ID和AccessKey Secret换成阿里云真实的点击「保存」→「部署代码」橙色按钮必须点✅ 第二步强制清除浏览器缓存关键浏览器会缓存旧的响应头就算改了代码旧缓存还会让它继续下载。按Ctrl Shift Delete清除浏览器缓存只清「缓存的图片和文件」即可或者用无痕模式打开链接完全避开缓存访问链接https://audio-miaomiao-mpwulqeoed.cn-chengdu.fcapp.run/we.html✅ 第三步终极兜底如果还下载直接用这个方案如果上面改完还下载说明函数计算的响应头被覆盖了直接用静态文件直出方案100% 生效1. 把we.html直接嵌入函数代码不用读 OSS彻底绕开问题替换app.py为下面的代码把we.html内容直接写进去我用占位符替换成自己的加密版we.html代码from flask import Flask, Response app Flask(__name__) # 把we.html完整代码加密版直接写在这里 HTML_CONTENT !DOCTYPE html html head meta charsetUTF-8 title验证页面/title !-- 完整we.html代码包括加密的答案、JS逻辑 -- /head body !-- 页面内容 -- /body /html # 直接返回网页强制Content-Type绝对不会下载 app.route(/we.html) def get_html(): return Response( HTML_CONTENT, content_typetext/html; charsetutf-8, headers{Cache-Control: no-cache} ) if __name__ __main__: app.run(host0.0.0.0, port9000)部署后访问就不会再下载直接渲染页面