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

通过Post接口上传至Atlassian Jira的附件为空问题求助

Jira附件上传后为空的解决方法
  • 验证Base64源数据有效性
    先确认docbody对应的Base64字符串没有多余前缀(比如data:image/png;base64,这类格式前缀),且解码后的字节数组非空。可以在代码里加判断:

    byte[] fileBytes = Convert.FromBase64String(docbody);
    if (fileBytes.Length == 0)
    {
        Console.WriteLine("Base64解码后字节数组为空");
        return;
    }
    
  • 为文件内容设置正确的Content-Type
    Jira需要明确文件的MIME类型才能正确解析内容,根据你上传的文件类型给fileContent1添加对应头信息,比如Word文档:

    HttpContent fileContent1 = new ByteArrayContent(fileBytes);
    fileContent1.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
    

    其他常见类型参考:PDF用application/pdf,普通文本用text/plain,图片用image/png/image/jpeg。

  • 修正后的完整代码示例

    string postUrl = $"{url}/rest/api/2/issue/{issuekey}/attachments";
    MultipartFormDataContent content = new MultipartFormDataContent();
    
    client.DefaultRequestHeaders.Add("X-Atlassian-Token", "nocheck");
    
    byte[] fileBytes = Convert.FromBase64String(docbody);
    if (fileBytes.Length == 0)
    {
        Console.WriteLine("源Base64数据无效");
        return;
    }
    
    HttpContent fileContent1 = new ByteArrayContent(fileBytes);
    // 根据实际文件类型修改Content-Type
    fileContent1.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
    content.Add(fileContent1, "file", filename);
    
    // 建议用await替代.Result避免线程阻塞问题
    var response2 = await client.PostAsync(postUrl, content);
    
    if (response2.IsSuccessStatusCode)
    {
        Console.WriteLine("OK");
    }
    else
    {
        // 打印响应内容方便排查问题
        string responseContent = await response2.Content.ReadAsStringAsync();
        Console.WriteLine($"上传失败:{responseContent}");
    }
    
  • 额外排查点

    • 如果是文本类文件,确认Base64编码时使用了正确的字符编码(比如UTF-8)
    • 检查当前Jira用户是否拥有对应 issue 的附件上传权限
    • 查看Jira服务器日志,是否存在文件解析相关的错误记录

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 21:01:05