本文介绍了用于SharePoint Online的CSOM的备用Save/OpenBinaryDirect方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于MS

解决方案

在.NET Core CSOM中下载文件:

 使用(var authenticationManager = new AuthenticationManager())使用(var context = authenticationManager.GetContext(site,user,password)){context.Load(context.Web,p => p.Title);context.ExecuteQuery();Microsoft.SharePoint.Client.File文件= context.Web.GetFileByUrl("https://tenant.sharepoint.com/sites/michael/Shared%20Documents/aa.txt");context.Load(文件);context.ExecuteQuery();字符串filepath = @" C:\ temp \"+ file.Name;Microsoft.SharePoint.Client.ClientResult< Stream>mstream = file.OpenBinaryStream();context.ExecuteQuery();使用(var fileStream = new System.IO.FileStream(filepath,System.IO.FileMode.Create)){mstream.Value.CopyTo(fileStream);}使用(System.IO.StreamReader sr = new System.IO.StreamReader(mstream.Value)){字符串行= sr.ReadToEnd();Console.WriteLine(line);}} 

在.NET Core CSOM中上传文件:

 字符串文件路径= @"C:\ temp \ aa.txt";FileCreationInformation newfile =新的FileCreationInformation();newfile.Url = System.IO.Path.GetFileName(filepath);newfile.Content = System.IO.File.ReadAllBytes(filepath);列表库= context.Web.Lists.GetByTitle("Documents");Microsoft.SharePoint.Client.File uploadFile = library.RootFolder.Files.Add(newfile);context.Load(uploadFile);context.ExecuteQuery(); 

Based on the doc from MS https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/using-csom-for-dotnet-standard, Save/OpenBinaryDirect methods is not available for .NET core app, they suggest to use regular file API, so what is the alternative way to read/write files stored in SharePoint online? what is the regular file API? does anyone done this? any example code/documentation?

解决方案

Download file in .NET Core CSOM:

  using (var authenticationManager = new AuthenticationManager())
  using (var context = authenticationManager.GetContext(site, user, password))
  {
    context.Load(context.Web, p => p.Title);
    context.ExecuteQuery();
    Microsoft.SharePoint.Client.File file = context.Web.GetFileByUrl("https://tenant.sharepoint.com/sites/michael/Shared%20Documents/aa.txt");
    context.Load(file);
    context.ExecuteQuery();       
    string filepath = @"C:\temp\" + file.Name;



    Microsoft.SharePoint.Client.ClientResult<Stream> mstream = file.OpenBinaryStream();
    context.ExecuteQuery();
    
    using (var fileStream = new System.IO.FileStream(filepath, System.IO.FileMode.Create))
    {
      mstream.Value.CopyTo(fileStream);
    }


    using (System.IO.StreamReader sr = new System.IO.StreamReader(mstream.Value))
    {
      String line = sr.ReadToEnd();
      Console.WriteLine(line);
    }

  }

Upload file in .NET Core CSOM:

string filepath = @"C:\temp\aa.txt";
FileCreationInformation newfile = new FileCreationInformation();
newfile.Url = System.IO.Path.GetFileName(filepath);
newfile.Content= System.IO.File.ReadAllBytes(filepath);

List library = context.Web.Lists.GetByTitle("Documents");
Microsoft.SharePoint.Client.File uploadFile = library.RootFolder.Files.Add(newfile);
context.Load(uploadFile);
context.ExecuteQuery();

这篇关于用于SharePoint Online的CSOM的备用Save/OpenBinaryDirect方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 07:48