Ejemplo n.º 1
0
        /// <summary>
        /// the maximum bytes in any given request is less than 60 MiB.
        /// </summary>
        /// <param name="token"></param>
        /// <param name="hostName"></param>
        /// <param name="siteCollectionId"></param>
        /// <param name="siteId"></param>
        /// <param name="driveId"></param>
        /// <param name="fileName"></param>
        public static void UploadNewLargeFile(string token, string hostName, Guid siteCollectionId, Guid siteId, string driveId, string fileName, string filePath)
        {
            string webApiUrl = string.Format("{0}/sites/{1},{2},{3}/drives/{4}/root:/{5}:/createUploadSession", IsBeta ? GraphAPIVersion.BETA : GraphAPIVersion.V1, hostName, siteCollectionId, siteId, driveId, fileName);

            dynamic result    = GraphApiCallHelper.PostApiJObject(token, webApiUrl, "");
            var     uploadUrl = result.uploadUrl.ToString();

            using (var stream = new FileStream(filePath, FileMode.Open))
                using (BinaryReader br = new BinaryReader(stream))
                {
                    var    length = stream.Length;
                    byte[] buffer = new byte[1024];
                    //byte[] buffer = new byte[2 * 1024 * 1024];
                    int  bytesRead      = 0;
                    long totalBytesRead = 0;
                    while ((bytesRead = br.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        dynamic uploadResult = null;
                        totalBytesRead = totalBytesRead + bytesRead;
                        if (totalBytesRead == length)
                        {
                            // Copy to a new buffer that has the correct size
                            var lastBuffer = new byte[bytesRead];
                            Array.Copy(buffer, 0, lastBuffer, 0, bytesRead);
                            buffer = lastBuffer;
                        }
                        uploadResult = GraphApiCallHelper.PutApiUploadLargeFileJObject(token, uploadUrl, buffer, string.Format("bytes {0}-{1}/{2}", totalBytesRead - bytesRead, totalBytesRead - 1, length));
                    }
                }
        }