如何为Upload Session Microsoft Graph API格式化Content-Range标头

Bri*_*ith 2 sharepoint typescript microsoft-graph

我遇到了使用v1.0 Microsoft Graph API将文件上传到上传会话的问题.我的步骤:

  1. 获取访问令牌:成功
  2. 选择文件/创建上传会话:成功(我返回uploadUrl上传字节)
  3. 将字节上传到uploadUrl:失败并出现以下错误.

这是我收到的错误:

{
   "error": 
   {
      "code":"invalidRequest",
      "message":"The Content-Range header is missing or malformed."
   }
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下格式,但仍然收到相同的错误.

"bytes 0-100/100"
"0-100/100"
"0-100"
Run Code Online (Sandbox Code Playgroud)

我正在关注Mircosoft的这篇文章https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/item_createuploadsession

这是我的函数,上传到uploadUrl从创建上传会话中检索

public uploadToSession(file: HTMLInputElement, session: any, fileData: any) {
    var headers = new Headers();
    headers.append('Content-Length', fileData.length); //(ie: "100")
    headers.append('Content-Range', fileData.range); //(ie: "bytes 0-100/100")
    this.http.put(session.uploadUrl, { headers })
        .subscribe(
            (res: Response) => console.log(res),
            error => console.log(error)
        );
}
Run Code Online (Sandbox Code Playgroud)

任何帮助深表感谢!提前致谢.

Bra*_*rad 9

Content-Range首标具有以下格式:

Content-Range: bytes <startindex>-<endindex>/<totallength>
Run Code Online (Sandbox Code Playgroud)

您的问题似乎是索引(基于0)和长度(基于1)的混合.对于一个100字节的文件,您的标题需要如下所示:

Content-Range: bytes 0-99/100
Run Code Online (Sandbox Code Playgroud)

  • `content.Headers.ContentRange = new ContentRangeHeaderValue( 0, source.Length-1, source.Length);` (2认同)