C# 使用graph API上载到MS团队的文档已损坏

C# 使用graph API上载到MS团队的文档已损坏,c#,microsoft-graph-api,microsoft-teams,C#,Microsoft Graph Api,Microsoft Teams,我正在尝试使用Microsoft Graph(测试版)将文档上载到Microsoft团队,但成功上载后文档会损坏 使用Graph,我首先创建一个组,基于该组创建一个团队,添加一些团队成员,最后将文档上传到默认频道 除了上传的文档被破坏和Office Online editor无法打开之外,所有工作正常。但是,我们可以下载该文件并在更正该文件后在Microsoft Word中打开 下面是我用于文档上传的代码-> FileInfo fileInfo = new FileInfo(@"F:\

我正在尝试使用Microsoft Graph(测试版)将文档上载到Microsoft团队,但成功上载后文档会损坏

使用Graph,我首先创建一个组,基于该组创建一个团队,添加一些团队成员,最后将文档上传到默认频道

除了上传的文档被破坏和Office Online editor无法打开之外,所有工作正常。但是,我们可以下载该文件并在更正该文件后在Microsoft Word中打开

下面是我用于文档上传的代码->

FileInfo fileInfo = 
    new FileInfo(@"F:\Projects\TestProjects\MSTeamsSample\MSTeamsSample\Files\Test File.docx");

var bytes = System.IO.File.ReadAllBytes(fileInfo.FullName);
var endpoint = $"https://graph.microsoft.com/beta/groups/{groupId}/drive/items/root:/General/{fileInfo.Name}:/content";

var fileContent = new ByteArrayContent(bytes);
fileContent.Headers.ContentType = 
    MediaTypeHeaderValue.Parse("application/octet-stream");

var requestContent = new MultipartFormDataContent();
requestContent.Add(fileContent, "File", fileInfo.Name);

var request = new HttpRequestMessage(HttpMethod.Put, endpoint);
request.Headers.Authorization = 
    new AuthenticationHeaderValue("Bearer", "<Access Token>");
request.Content = requestContent;
var client = new HttpClient();
var response = client.SendAsync(request).Result;
FileInfo FileInfo=
新文件信息(@“F:\Projects\TestProjects\MSTeamsSample\MSTeamsSample\Files\testfile.docx”);
var bytes=System.IO.File.ReadAllBytes(fileInfo.FullName);
变量端点=$”https://graph.microsoft.com/beta/groups/{groupId}/drive/items/root:/General/{fileInfo.Name}:/content”;
var fileContent=newbytearraycontent(字节);
fileContent.Headers.ContentType=
MediaTypeHeaderValue.Parse(“应用程序/八位字节流”);
var requestContent=new MultipartFormDataContent();
添加(fileContent,“File”,fileInfo.Name);
var request=newhttprequestmessage(HttpMethod.Put,endpoint);
request.Headers.Authorization=
新的AuthenticationHeaderValue(“持票人”)将非常感谢您的帮助。

请尝试以下内容:

        var filePath = @"F:\Projects\TestProjects\MSTeamsSample\MSTeamsSample\Files\Test File.docx";
        var fileName = Path.GetFileName(filePath);
        var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
        var endpoint = $"https://graph.microsoft.com/beta/groups/{groupId}/drive/items/root:/General/{fileName}:/content";

        using (var client = new HttpClient())
        {
            using (var content = new StreamContent(fileStream))
            {
                content.Headers.Add("Content-Type", MimeMapping.GetMimeMapping(fileName));

                // Construct the PUT message towards the webservice
                using (var request = new HttpRequestMessage(HttpMethod.Put, endpoint))
                {
                    request.Content = content;

                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokenResponse.Token);

                    // Request the response from the webservice
                    using (var response = await client.SendAsync(request))
                    {
                        // Check the response.
                    }
                }
            }
        }

我可以在Microsoft Team editor中看到Word文档。

您能解释一下“我们可以在更正文件后下载文件并在Microsoft Word中打开”的意思吗"?您正在进行什么更正?从MS团队下载该文件后,当您尝试打开它时,MS word会提示您该文件已损坏。它还表示word可以尝试更正该文件并显示内容。通过这样做,您仍然可以看到该文件的内容。它起到了作用。感谢您的解决方案,但我也想了解如何使用多部分文件上载使其工作。