[英]Upload > 5 MB files to sharepoint 2013 programmatically
我在將大文件上傳到我的sharepoint 2013 / office 365網站時遇到麻煩。 我正在使用Visual Stuidos 2010和.NET 4.0
我從以下問題嘗試過代碼:
SP2010客戶端對象模型3 MB限制-更新maxReceivedMessageSize沒有應用
通過c#Web服務將100mb +的大文件上傳到Sharepoint 2010
如何使用CSOM從SharePoint 2013下載文件/向SharePoint 2013上傳文件?
但是沒有任何效果。 所以我需要一點幫助。 這是我嘗試過的代碼:
1 :(我也曾嘗試為此使用SharePointOnlineCredentials
而不是NetworkCredential
)
#region 403 forbidden
byte[] content = System.IO.File.ReadAllBytes(fileInfo.FullName);
System.Net.WebClient webclient = new System.Net.WebClient();
System.Uri uri = new Uri(sharePointSite + directory + fileInfo.Name);
webclient.Credentials = new NetworkCredential(user, password.ToString(), sharePointSite + "Documents");
webclient.UploadData(uri, "PUT", content);
#endregion
2:
#region 500 Internal Server Error
using (var fs = new FileStream(fileInfo.FullName, FileMode.Open))
{
Microsoft.SharePoint.Client.File.SaveBinaryDirect(
context,
web.ServerRelativeUrl + "/" + directory,
fs,
true);
}
#endregion
我已經上傳了一些較小的文件來使用:
#region File upload for smaller files
Folder folder = context.Web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + directory);
web.Context.Load(folder);
context.ExecuteQuery();
FileCreationInformation fci = new FileCreationInformation();
fci.Content = System.IO.File.ReadAllBytes(fileInfo.FullName);
fciURL = sharePointSite + directory;
fciURL += (fciURL[fciURL.Length - 1] == '/') ? fileInfo.Name : "/" + fileInfo.Name;
fci.Url = fciURL;
fci.Overwrite = true;
Microsoft.SharePoint.Client.FileCollection documentfiles = folder.Files;
context.Load(documentfiles);
context.ExecuteQuery();
Microsoft.SharePoint.Client.File file = documentfiles.Add(fci);
context.Load(file);
context.ExecuteQuery();
#endregion
我的使用聲明:
using (Microsoft.SharePoint.Client.ClientContext context = new Microsoft.SharePoint.Client.ClientContext(sharePointSite))
{
//string fciURL = "";
exception = "";
context.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(user, password);
Web web = context.Web;
web.Context.Credentials = context.Credentials;
if (!web.IsPropertyAvailable("ServerRelativeUrl"))
{
web.Context.Load(web, w => w.ServerRelativeUrl);
web.Context.ExecuteQuery();
}
//upload large file
}
我去的解決方案:
MemoryStream destStream;
using (System.IO.FileStream fInfo = new FileStream(fileInfo.FullName, FileMode.Open))
{
byte[] buffer = new byte[16 * 1024];
byte[] byteArr;
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = fInfo.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
byteArr = ms.ToArray();
}
destStream = new MemoryStream(byteArr);
Microsoft.SharePoint.Client.File.SaveBinaryDirect(
context,
serverRelativeURL + directory + fileInfo.Name,
destStream,
true);
context.ExecuteQuery();
results = "File Uploaded";
return true;
}
您的代碼段編號2的問題是您缺少文件名:
using (var fs = new FileStream(fileInfo.FullName, FileMode.Open))
{
Microsoft.SharePoint.Client.File.SaveBinaryDirect(
context,
serverRelativeURL + directory + fs.Name,
^^^^^^^^
fs,
true);
}
我對該主題的研究表明,使用FrontPage遠程控制過程是可靠上傳大文件的最冒險的方法。
這是因為FrontPage RPC支持文件碎片,這有助於避免OutOfMemomory
異常,因為Windows需要將整個文件分配給連續內存。
它還支持發送元數據,幾乎對任何文件上傳應用程序都很有用。 這樣的一個主要優點是,您實際上可以指定正確的內容類型,而無需用戶登錄並稍后進行更改。 (我嘗試過的所有其他方法都將被設置為默認類型。)
有關實現Frontpage RPC的更多詳細信息, 請參見我在Sharepoint StackExchange上的答案 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.