51CTO 博客地址:https://blog.51cto.com/13969817
博客园博客地址:https://www.cnblogs.com/bxapollo
很多大型企业使用很多数据平台存储数据,比如File 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<long> 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<long> bytesUploaded = f.ContinueUpload(uploadId, fileoffset, ss);
context.ExecuteQuery();
fileoffset = bytesUploaded.Value;
}
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
这里分享此解决方案,希望对大家日后有所帮助,如果有疑问,欢迎线下交流。