none
Download large files using REST RRS feed

  • Question

  • Previous questions on this subject point a way using RPC. This is to download files as large as 9GB. Isn't there a way to do the same in REST? Csom way of doing opening binary streams has some limits, I would expect and so looking for a way in REST.

    I was trying to download some sample file in 'chunks' from this endpoint -

        {0}/_api/web/GetFileByServerRelativeUrl('{1}/$value

    but the file was downloaded fully not in chunks. Is it that Sharepoint Online doesn't support "Range" to supply the byte range am looking for? Please help.

    Saturday, March 18, 2017 12:47 PM

All replies

  • Hi,

    It is seems not supported to use chunk download file using REST API from SharePoint Online , here is a similar thread for your reference:

    http://sharepoint.stackexchange.com/questions/188419/download-partial-by-using-getfilebyid-sharepoint-rest-api

    As you said, we can use RPC to achieve it.

    string fullFilePath = String.Empty;
     
    Uri targetSite = new Uri(ctx.Web.Url);
     
    SharePointOnlineCredentials spCredentials = (SharePointOnlineCredentials)ctx.Credentials;
    string authCookieValue = spCredentials.GetAuthenticationCookie(targetSite);
     
    string requestUrl = ctx.Url + "/_vti_bin/_vti_aut/author.dll";
    string method = Utility.GetEncodedString("get document:15.0.0.4455");
    serviceName = Utility.GetEncodedString(ctx.Web.ServerRelativeUrl);
    if(documentName.StartsWith("/"))
    {
        documentName = documentName.Substring(1);
    }
    documentName = Utility.GetEncodedString(documentName);
    string oldThemeHtml = "false";
    string force = "true";
    string getOption = "none";
    string docVersion = String.Empty; //directly passed as empty
    string timeOut = "0";
    string expandWebPartPages = "true";
     
    string rpcCallString = String.Format("method={0}&service%5fname={1}&document%5fname={2}&old%5ftheme%5fhtml={3}&force={4}&get%5foption={5}&doc%5fversion=&timeout={6}&expandWebPartPages={7}",
        method, serviceName, documentName, oldThemeHtml, force, getOption, timeOut, expandWebPartPages);
     
    HttpWebRequest wReq = WebRequest.Create(requestUrl) as HttpWebRequest;
    wReq.Method = "POST";
    wReq.ContentType = "application/x-vermeer-urlencoded";
    wReq.Headers["X-Vermeer-Content-Type"] = "application/x-vermeer-urlencoded";
    wReq.UserAgent = "MSFrontPage/15.0";
    wReq.UseDefaultCredentials = false;
    wReq.Accept = "auth/sicily";
    wReq.Headers["MIME-Version"] = "1.0";
    wReq.Headers["X-FORMS_BASED_AUTH_ACCEPTED"] = "T";
    wReq.Headers["Accept-encoding"] = "gzip, deflate";
    wReq.Headers["Cache-Control"] = "no-cache";
     
    wReq.CookieContainer = new CookieContainer();
    wReq.CookieContainer.Add(
        new Cookie("SPOIDCRL",
            authCookieValue.TrimStart("SPOIDCRL=".ToCharArray()),
            String.Empty,
            targetSite.Authority));
     
    wReq.KeepAlive = true;
     
    //create unique dir for the download
    DirectoryInfo tempFilePath = Directory.CreateDirectory(Path.Combine(tempFileLoc, Guid.NewGuid().ToString()));
     
    using (Stream requestStream = wReq.GetRequestStream())
    {
        byte[] rpcHeader = Encoding.UTF8.GetBytes(rpcCallString);
     
        requestStream.Write(rpcHeader, 0, rpcHeader.Length);
        requestStream.Close();
     
        fullFilePath = Path.Combine(tempFilePath.FullName, fileName);
     
        using (Stream strOut = File.OpenWrite(fullFilePath))
        {
            using (var sr = wReq.GetResponse().GetResponseStream())
            {
                byte[] buffer = new byte[16 * 1024];
                int read;
                bool isHtmlRemoved = false;
                while ((read = sr.Read(buffer, 0, buffer.Length)) > 0)
                {
                    if(!isHtmlRemoved)
                    {
                        string result = Encoding.UTF8.GetString(buffer);
                        int startPos =result.IndexOf("</html>");
                        if(startPos >-1)
                        {
                            //get the length of the text, '</html>' as well
                            startPos += 8;
                             
                            strOut.Write(buffer, startPos, read - startPos);
     
                            isHtmlRemoved = true;
                        }                                    
                    }
                    else
                    {
                        strOut.Write(buffer, 0, read);
                    }
                }
            }
        }
    }

    https://realmpksharepoint.wordpress.com/2016/08/15/download-large-files-from-sharepoint-online/

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, March 20, 2017 6:58 AM
    Moderator