使用图形 API 上传到 MS Teams 的文档已损坏 [英] Document uploaded to MS Teams using graph API gets corrupted

查看:76
本文介绍了使用图形 API 上传到 MS Teams 的文档已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Microsoft Graph(测试版)将文档上传到 Microsoft Teams,但在成功上传后文档已损坏.

I am trying to upload a document to Microsoft Teams using Microsoft Graph (beta version), but the document gets corrupted after a successful upload.

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

Using Graph, I'm first creating an Group, creating a Team based on the Group, adding some Team Members and finally uploading a document to the default channel.

除了上传的文档损坏并且 Office Online 编辑器无法打开之外,一切正常.但是,我们可以下载文件并在更正文件后在 Microsoft Word 中打开.

All works fine except the uploaded document gets corrupted and the Office Online editor is not able to open it. We can however download the file and open in Microsoft Word after correcting the file.

以下是我用于文件上传的代码->

Below is the code that I'm using for document upload->

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;

我尝试将内容类型更改为 application/vnd.openxmlformats-officedocument.wordprocessingml.document 但没有成功.我不明白这里有什么问题.代码非常简单,基于此 文档.任何帮助将不胜感激.

I tried changing content type to application/vnd.openxmlformats-officedocument.wordprocessingml.document but no luck. I don't understand what could be wrong here. The code is pretty straight forward, based on the this documentation. Any help will be highly appreciated.

推荐答案

请试试这个:

        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 Teams 编辑器中看到 Word 文档.

I am able to see Word document in Microsoft Teams editor.

这篇关于使用图形 API 上传到 MS Teams 的文档已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆