2014-02-26 78 views
2

我遇到了上傳大文件到我的SharePoint 2013/Office 365站點的麻煩。我使用Visual Stuidos 2010和.NET 4.0上傳>以編程方式將5 MB文件上傳到sharepoint 2013

我曾嘗試代碼這些問題:

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:(I也試圖使用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 
} 

回答

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; 
} 
+0

您不需要調用'context.ExecuteQuery()'方法。 – Warlock

+0

我相信你應該刪除所有MemoryStream相關的方法,並把'fInfo'流到'SaveBinaryDirect()'方法。 – Warlock

+1

它適用於Office365(SharePoint Online)嗎? – Warlock

0

與您的代碼段2號的問題是,你錯過了一個文件名:

using (var fs = new FileStream(fileInfo.FullName, FileMode.Open)) 
{ 
    Microsoft.SharePoint.Client.File.SaveBinaryDirect(
     context, 
     serverRelativeURL + directory + fs.Name, 
             ^^^^^^^^ 
     fs, 
     true); 
} 
0

我關於這個主題的研究表明,使用FrontPage Remote Control Procedures是可靠上傳大文件的最有效辦法。

這是因爲FrontPage RPC支持文件碎片,這有助於避免OutOfMemomory異常,因爲Windows需要將整個文件分配給連續內存。

它還支持發送元數據,它非常適用於任何文件上傳應用程序。這樣做的一個主要優點是,您實際上可以指定正確的內容類型,而無需用戶登錄並稍後進行更改。 (我嘗試過的所有其他方法只會設置爲默認類型)。

See my answer on the Sharepoint StackExchange有關實現Frontpage RPC的更多詳細信息。

相關問題