C# – Upload > 5 MB files to sharepoint 2013 programmatically

csharepointsharepoint-2010sharepoint-2013wpf

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 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?

But nothing is working. So I need a little help. Here is code that I have tried:

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

I have gotten smaller file uploads to work with:

#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

My Using Statement:

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
}

Best Answer

The solution I went with:

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;
}
Related Topic