我无法将大型文件上传到我的sharepoint 2013 / office 365网站。我正在使用Visual Stuidos 2010和.NET 4.0
我尝试过这些问题的代码:
SP2010 Client Object Model 3 MB limit - updating maxReceivedMessageSize doesnt get applied
maximum file upload size in sharepoint
Upload large files 100mb+ to Sharepoint 2010 via c# Web Service
How to download/upload files from/to SharePoint 2013 using CSOM?
但没有任何效果。所以我需要一些帮助。这是我尝试过的代码:
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
}
答案 0 :(得分:1)
我选择的解决方案:
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;
}
答案 1 :(得分:0)
您的代码段2的问题在于您错过了文件名:
using (var fs = new FileStream(fileInfo.FullName, FileMode.Open))
{
Microsoft.SharePoint.Client.File.SaveBinaryDirect(
context,
serverRelativeURL + directory + fs.Name,
^^^^^^^^
fs,
true);
}
答案 2 :(得分:0)
我对该主题的研究表明,使用FrontPage Remote Control Procedures是可靠地上传大文件的最有效方式。
这是因为FrontPage RPC支持文件碎片,这有助于避免由于Windows需要将整个文件分配给连续内存而导致的OutOfMemomory
异常。
它还支持发送元数据,几乎适用于任何文件上传应用程序。这样做的一个主要优点是,您实际上可以指定正确的内容类型,而无需用户登录并在以后进行更改。 (使用我尝试过的所有其他方法,只需将其设置为默认类型。)
See my answer on the Sharepoint StackExchange有关实现Frontpage RPC的更多详细信息。