SharePoint 解决方案:SharePoint Server API无法上传大于2GB的文档

网友投稿 303 2022-10-03

SharePoint 解决方案:SharePoint Server API无法上传大于2GB的文档

51CTO 博客地址:Server,由于管理平台较多,给IT造成很大压力,因为维护成本高,维护系统也多,所以为了便于IT的管理,同时方便用户统一数据平台来访问数据,IT会考虑将多个系统统一到一个强壮且安全的系统管理和维护。

今天跟Team在尝试使用SharePoint Server API(Server Object Model)将File Server中的档案上传到SharePoint端,同时keep属性和权限,但发现Server API无法上传单个大于2GB档案上到SharePoint Server 2019。

调用Microsoft.SharePoint.dll的方法:Microsoft.SharePoint.SPFileCollection.Add(SPResourcePath filePath, Stream file, SPFileCollectionAddParameters parameters)

Bug分析如下:通过反射发现里面有个很明显的bug,属于Microsoft.SharePoint.dll内部方法限制

解决方案:使用SharePoint Client API(Client Object model)来完成大于2GB的数据上载,具体Sample Code如下所示:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Security; using System.IO; using System.Net; using Microsoft.SharePoint.Client; using Microsoft.SharePoint.Client.Administration; using Microsoft.SharePoint.Client.Application; namespace ConsoleApp1 { class Program { static void Main(string[] args) { try { ClientContext context = new ClientContext("http://contoso-sp2019/sites/Test"); string userName = "XXXX"; string pwd = "XXXXX"; string domain = "contoso.com"; SecureString pwdd = new SecureString(); pwd.ToList().ForEach(pwdd.AppendChar); //SharePointOnlineCredentials credential = new SharePointOnlineCredentials(userName, pwdd); NetworkCredential credential = new NetworkCredential(userName, pwdd, domain); context.Credentials = credential; List library = context.Web.GetList("http://contoso-sp2019/sites/Test/Shared%20Documents"); context.Load(library); context.ExecuteQuery(); FileCreationInformation fileInfo = new FileCreationInformation(); fileInfo.Url = "http://contoso-sp2019/sites/Test/Shared%20Documents/abc.zip"; fileInfo.Content = new byte[0]; Microsoft.SharePoint.Client.File f = library.RootFolder.Files.Add(fileInfo); context.Load(f); context.ExecuteQuery(); Guid uploadId = Guid.NewGuid(); using (FileStream fs = new FileStream("abc.zip", FileMode.Open)) { byte[] buffer = new byte[1 * 1024 * 1024]; Byte[] lastBuffer = null; long fileoffset = 0; long totalBytesRead = 0; int bytesRead; bool first = true; bool last = false; while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0) { totalBytesRead = totalBytesRead + bytesRead; if (totalBytesRead == fs.Length) { last = true; lastBuffer = new byte[bytesRead]; Array.Copy(buffer, 0, lastBuffer, 0, bytesRead); } if (first) { using (MemoryStream contentStream = new MemoryStream()) { using (MemoryStream s = new MemoryStream(buffer)) { ClientResult bytesUploaded = f.StartUpload(uploadId, s); context.ExecuteQuery(); fileoffset = bytesUploaded.Value; } first = false; } } else { if (last) { using (MemoryStream ss = new MemoryStream(lastBuffer)) { f.FinishUpload(uploadId, fileoffset, ss); context.ExecuteQuery(); break; } } else { using (MemoryStream ss = new MemoryStream(buffer)) { ClientResult bytesUploaded = f.ContinueUpload(uploadId, fileoffset, ss); context.ExecuteQuery(); fileoffset = bytesUploaded.Value; } } } } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } } }

这里分享此解决方案,希望对大家日后有所帮助,如果有疑问,欢迎线下交流。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Microsoft 365:OneDrive 链接失败0x8804de40解决方案
下一篇:Java虚拟机启动过程探索
相关文章

 发表评论

暂时没有评论,来抢沙发吧~