上传> 5 MB的文件到SharePoint 2013编程 [英] Upload > 5 MB files to sharepoint 2013 programmatically
问题描述
我有麻烦上传大文件到我的sharepoint 2013 / office 365网站。我使用Visual Stuidos 2010和.NET 4.0
I am having troubles uploading large files to my sharepoint 2013/office 365 site. I am using Visual Stuidos 2010 and .NET 4.0
我已经尝试过这些问题的代码:
I have tried code from these questions:
SP2010客户端对象模型3 MB限制 - 更新maxReceivedMessageSize does not get应用
Upload large files 100mb+ to Sharepoint 2010 via c# Web Service
如何使用CSOM从/向SharePoint 2013下载/上传文件?
但没有什么工作。所以我需要一点帮助。这里是我试过的代码:
But nothing is working. So I need a little help. Here is code that I have tried:
1(我也试图使用 SharePointOnlineCredentials
,而不是 NetworkCredential
)
1: ( I have also tried to use SharePointOnlineCredentials
instead of NetworkCredential
for this one)
#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;
}
这篇关于上传> 5 MB的文件到SharePoint 2013编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!