0

我正在尝试使用 Graph API 将文件上传到 SharePoint,为了执行该操作,我需要将文件转换为二进制流并将该值传递给我的图形 api 请求正文。有人可以帮我解决这个问题吗?

4

1 回答 1

0

@Rishi Roshan,

请参考以下控制台应用程序:

using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharePointSite.msgraph
{
    class UploadFile
    {
        static void Main(string[] args)
        {
            string clientId = "e0cefc2c-1004-4622-81ab-f7b421063112"; //e.g. 01e54f9a-81bc-4dee-b15d-e661ae13f382
            
            IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
            .Create(clientId)
            .WithRedirectUri("msale0cefc2c-1104-4622-81ab-f7b421063112://auth")
            .Build();

            string[] scopes = new string[] { "Sites.ReadWrite.All" };
            InteractiveAuthenticationProvider authProvider = new InteractiveAuthenticationProvider(publicClientApplication, scopes);


            GraphServiceClient graphClient = new GraphServiceClient(authProvider);

            using var stream = System.IO.File.OpenRead(@"C:\Users\admin\Downloads\test.txt");
            DriveItem uploadfile = graphClient.Sites.Root.Drive.Root.ItemWithPath("test-111.txt").Content.Request().PutAsync<DriveItem>(stream).Result;

            Console.WriteLine(uploadfile);
        }
    }
}

BR

于 2020-11-13T05:47:59.643 回答