You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Streamlit应用上传图片时出现AxiosError: 403状态码错误求助

排查Streamlit上传图片时的AxiosError 403问题

问题描述

在使用Streamlit开发图片查看器时,点击上传图片触发AxiosError: Request failed with status code 403错误。已确认所有依赖包安装完整,且在全新虚拟环境中运行项目,问题仍未解决。

涉事代码

import streamlit as st
from PIL import Image

# Title
st.title('Image Viewer')

# Upload an image
uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png", "webp"])

# Check the type of the uploaded image
if uploaded_image is not None:
    # Open the image using PIL
    image = Image.open(uploaded_image)
    # Display the image
    st.image(image, use_column_width=True)

排查与解决方法

  • 升级/回退Streamlit版本
    部分Streamlit版本存在文件上传相关的已知bug,优先尝试升级到最新稳定版:

    pip install --upgrade streamlit
    

    若最新版仍报错,可回退到验证过的稳定版本(例如1.28.0)测试:

    pip install streamlit==1.28.0
    
  • 排查网络代理与防火墙
    403状态码代表服务器拒绝请求,若你的网络环境存在代理或防火墙,可能拦截了Streamlit的内部请求:

    • 临时关闭代理、防火墙后重试上传
    • 若必须使用代理,运行Streamlit前配置代理环境变量:
      # Windows系统
      set HTTP_PROXY=http://你的代理地址:端口
      set HTTPS_PROXY=http://你的代理地址:端口
      
      # Linux/macOS系统
      export HTTP_PROXY=http://你的代理地址:端口
      export HTTPS_PROXY=http://你的代理地址:端口
      
  • 检查服务器端配置(若部署在远程)
    若应用部署在远程服务器,需确认反向代理(如Nginx)的配置:

    • 确保未限制上传文件的大小、类型
    • 验证CORS设置,允许Streamlit应用的请求来源
  • 极简环境测试
    创建仅包含文件上传组件的测试脚本,排除代码逻辑干扰:

    import streamlit as st
    st.file_uploader("测试上传", type=["png"])
    

    运行该脚本,若仍报错,说明问题出在环境或Streamlit本身;若正常,再排查原代码中的其他潜在冲突

  • 浏览器端排查

    • 清除浏览器缓存后重新打开应用
    • 检查浏览器是否禁用了该网站的文件上传权限,或阻止了相关JavaScript执行

内容的提问来源于stack exchange,提问作者harsh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 23:33:23