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());
}
}
}
}
這裏分享此解決方案,希望對大家日後有所幫助,如果有疑問,歡迎線下交流。