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

使用GitHub Gist上传的图片在GitHub Issue中无法正常显示的求助

问题根源

你当前的问题是把图片存成了Data URI格式的字符串(data:image/png;base64,...)到Gist里,导致Gist的raw链接返回的是纯文本内容,而非图片二进制数据,所以Markdown无法将其解析为图片。

修复方案

修改Gist上传逻辑:去掉Data URI前缀,直接传入图片的Base64编码内容,并添加encoding: 'base64'参数,让GitHub自动把Base64解码成二进制图片文件。这样raw链接返回的就是图片本身,Markdown就能正常渲染了。

修改后的完整代码
app.post('/create-issue',upload.single('file'), async (req, res) => {
  const file = req.file;
  const {title,issue} = req.body; 

  if (!file) return res.status(400).send('No file uploaded.');

  try {
    const filePath = path.join(__dirname, 'uploads', file.filename);
    const fileContent = fs.readFileSync(filePath);

    const response = await githubApi.post('/gists', {
      description: 'Issue image upload',
      public: true,
      files: {
        [file.originalname]: {
          content: fileContent.toString('base64'), // 仅传图片的Base64编码,去掉Data URI前缀
          encoding: 'base64' // 告诉GitHub这是Base64编码的二进制内容
        },
      },
    });
    
    const imageFile = response.data.files[file.originalname];
    const imageUrl = imageFile.raw_url;
    fs.unlinkSync(filePath);
    const issueBody = `${issue}\n\n![Error Image](${imageUrl})`;

    try {
      const _resp = await githubApi.post(`/repos/${GITHUB_ORG}/${GITHUB_REPO}/issues`, {
        title: title,
        body: issueBody,
      });

      res.json(_resp.data);
    } catch (error) {
      console.error('Error creating issue:', error.response.data);
      res.status(500).send('Failed to create issue.');
    }

  } catch (error) {
    console.error('Error uploading image:', error.response.data);
    res.status(500).send('Failed to upload image.');
  }
});
补充说明

你尝试用独立仓库存图片时遇到的409错误,是因为仓库分支开启了保护机制,直接提交文件必须通过Pull Request。用Gist的方案无需额外维护仓库,修改后就能满足需求。

内容的提问来源于stack exchange,提问作者Erdem Çağlak

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 14:13:16