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

SharePoint SPFx表单定制器集成Azure DevOps时附件上传下载异常

解决Azure DevOps附件上传损坏问题

问题根源

  1. 上传二进制文件时,请求头中额外添加了charset=utf-8,会破坏非文本类文件的二进制结构
  2. 附件内容可能以字符串形式传递,而非原始二进制数据(Blob/ArrayBuffer),导致文件编码异常

修复方案

1. 修正请求头的Content-Type

移除charset=utf-8,仅保留application/octet-stream——二进制文件无需字符编码声明:

const responseAttachment = await fetch(attachmentUrl, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/octet-stream', // 去掉charset=utf-8
        'Authorization': `Basic ${btoa(`:${this.pat}`)}`
    },
    body: attachment.Content
});

2. 确保附件内容为二进制格式

从SharePoint获取文件内容时,避免使用文本读取方式(如text()),改用arrayBuffer()或blob()获取原始二进制数据:

// 示例:从SharePoint获取文件内容
const fileResp = await fetch(sharePointFileUrl);
const fileContent = await fileResp.arrayBuffer(); // 或 blob()

3. 处理Base64编码的内容(如果适用)

如果attachment.Content是Base64字符串,需先转换为ArrayBuffer:

function base64ToBuffer(base64Str) {
    const binaryStr = atob(base64Str);
    const uint8Arr = new Uint8Array(binaryStr.length);
    for (let i = 0; i < binaryStr.length; i++) {
        uint8Arr[i] = binaryStr.charCodeAt(i);
    }
    return uint8Arr.buffer;
}

// 上传时替换body
body: base64ToBuffer(attachment.Content)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 17:55:08