3

I have a C# application in which, using the SharePoint.Client dll, I make client requests to SharePoint Online. Recently, as per this post, I was able to successfully upload a single 5GB file to SharePoint Online Document Library. However, I am unable to download the same! Here's what I have tried so far:

1) OpenBinaryDirect

FileInformation fileInformation = File.OpenBinaryDirect(clientContext, serverRelativeUrl);

The above line of code executes successfully for smaller files. But when I try to download my 5GB file, it throws an IOException: Stream was too long.

2) OpenBinaryStream

File oFile = web.GetFileByServerRelativeUrl(strServerRelativeURL);
clientContext.Load(oFile);
ClientResult<Stream> stream = oFile.OpenBinaryStream();
clientContext.ExecuteQuery();

There again, for the same set of smaller files, the above code also executes successfully and the files get downloaded easily. But the download of the 5GB file fails immediately at the ExecuteQuery line with a FormatException: Invalid MIME content-length header encountered on read.

Is there a way to download large files from SharePoint Online through code? I can directly download the file from the browser, but not sure how to do the same through code. Plz advice.

EDIT: 1 Here's a reference to a blog which confirms the fact that SharePoint now allows uploading files upto 10 GB each.

  • How did you uploaded the 5GB file in SharePoint? SharePoint simply allowed files to be uploaded upto 50MB size. – Hardik Jun 23 '16 at 7:41
  • @Hardik have you checked this link, https://dev.office.com/patterns-and-practices-detail/1876? SharePoint now supports the upload of a file as large as 10GB, previously it was 2GB only. Here, I tried it out with a 5GB file & was able to upload it successfully. The issue is, how to download the same through code. – Piyush Jun 23 '16 at 7:49
1

I understand this is old question, but it's shown in top search result so I think someone may want to get other answer.

Firstly, I implemented CookieAwareWebClient class

public class CookieAwareWebClient : WebClient
{
    public CookieContainer CookieContainer { get; set; } = new CookieContainer();

    protected override WebRequest GetWebRequest(Uri uri)
    {
        WebRequest request = base.GetWebRequest(uri);
        if (request is HttpWebRequest)
        {
            (request as HttpWebRequest).CookieContainer = CookieContainer;
        }
        return request;
    }
}

And then used it to make download request

public void DownloadFile(SP.File spoFile, string folderPath, ClientContext ctx)
{
    try
    {
        Uri targetSite = new Uri(ctx.Web.Url);
        SharePointOnlineCredentials spCredentials = (SharePointOnlineCredentials)ctx.Credentials;
        string authCookieValue = spCredentials.GetAuthenticationCookie(targetSite);
        using (CookieAwareWebClient wc = new CookieAwareWebClient())
        {
            wc.CookieContainer = new CookieContainer();
            wc.CookieContainer.Add(new Cookie("SPOIDCRL", authCookieValue.TrimStart("SPOIDCRL=".ToCharArray()), String.Empty, targetSite.Authority));
            string url = $"{ctx.Web.Url}/_layouts/download.aspx?SourceUrl={System.Web.HttpUtility.UrlEncode(spoFile.ServerRelativeUrl)}";
            wc.DownloadFile(url, Path.Combine(folderPath, spoFile.Name));
        }
    }
    catch (Exception ex)
    {
        //Error
    }
}

I've tested with 1GB file and it works

0

Finally, I was able to download files, up to 9.7GB from SharePoint Online using RPC. Please visit the following link, where I have blogged about this issue, to get the complete solution as it's not possible to post everything here.

Download Large Files from SharePoint Online

  • While it may seem long, you really do need to include at least the critical parts of an external resource in the answer, otherwise when your site goes down or you delete this article, the answer becomes useless. Further, I just checked, and there is plenty of space allotted for a single answer to cover everything you wrote in that article... and 10x more. – TylerH Aug 9 '18 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.