|
| 1 | +# Microsoft Graph .Net Client Library |
| 2 | + |
| 3 | +* [Overview](./overview.md) |
| 4 | +* [Collections](./collections.md) |
| 5 | +* [Errors](./errors.md) |
| 6 | +* [Contributions](./contributions.md) |
| 7 | +* [FAQ](./FAQ.md) |
| 8 | + |
| 9 | +## How do I work with... |
| 10 | + |
| 11 | +### OneDrive |
| 12 | + |
| 13 | +* [Download a large file from OneDrive](#downloadLargeFile) |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +<a name="downloadLargeFile"></a> |
| 21 | +### Download a large file from OneDrive |
| 22 | + |
| 23 | +```csharp |
| 24 | +// Based on question by Pavan Tiwari, 11/26/2012, and answer by Simon Mourier |
| 25 | +// https://stackoverflow.com/questions/13566302/download-large-file-in-small-chunks-in-c-sharp |
| 26 | +
|
| 27 | +const long DefaultChunkSize = 50 * 1024; // 50 KB, TODO: change chunk size to make it realistic for a large file. |
| 28 | +long ChunkSize = DefaultChunkSize; |
| 29 | +long offset = 0; // cursor location for updating the Range header. |
| 30 | +byte[] bytesInStream; // bytes in range returned by chunk download. |
| 31 | +
|
| 32 | +// Get the collection of drive items. We'll only use one. |
| 33 | +IDriveItemChildrenCollectionPage driveItems = await graphClient.Me.Drive.Root.Children.Request().GetAsync(); |
| 34 | + |
| 35 | +foreach (var item in driveItems) |
| 36 | +{ |
| 37 | + // Let's download the first file we get in the response. |
| 38 | + if (item.File != null) |
| 39 | + { |
| 40 | + // We'll use the file metadata to determine size and the name of the downloaded file |
| 41 | + // and to get the download URL. |
| 42 | + var driveItemInfo = await graphClient.Me.Drive.Items[item.Id].Request().GetAsync(); |
| 43 | + |
| 44 | + // Get the download URL. This URL is preauthenticated and has a short TTL. |
| 45 | + object downloadUrl; |
| 46 | + driveItemInfo.AdditionalData.TryGetValue("@microsoft.graph.downloadUrl", out downloadUrl); |
| 47 | + |
| 48 | + // Get the number of bytes to download. calculate the number of chunks and determine |
| 49 | + // the last chunk size. |
| 50 | + long size = (long)driveItemInfo.Size; |
| 51 | + int numberOfChunks = Convert.ToInt32(size / DefaultChunkSize); |
| 52 | + // We are incrementing the offset cursor after writing the response stream to a file after each chunk. |
| 53 | + // Subtracting one since the size is 1 based, and the range is 0 base. There should be a better way to do |
| 54 | + // this but I haven't spent the time on that. |
| 55 | + int lastChunkSize = Convert.ToInt32(size % DefaultChunkSize) - numberOfChunks - 1; |
| 56 | + if (lastChunkSize > 0) { numberOfChunks++; } |
| 57 | + |
| 58 | + // Create a file stream to contain the downloaded file. |
| 59 | + using (FileStream fileStream = System.IO.File.Create((@"C:\Temp\" + driveItemInfo.Name))) |
| 60 | + { |
| 61 | + for (int i = 0; i < numberOfChunks; i++) |
| 62 | + { |
| 63 | + // Setup the last chunk to request. This will be called at the end of this loop. |
| 64 | + if (i == numberOfChunks - 1) |
| 65 | + { |
| 66 | + ChunkSize = lastChunkSize; |
| 67 | + } |
| 68 | + |
| 69 | + // Create the request message with the download URL and Range header. |
| 70 | + HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, (string)downloadUrl); |
| 71 | + req.Headers.Range = new System.Net.Http.Headers.RangeHeaderValue(offset, ChunkSize + offset); |
| 72 | + |
| 73 | + // We can use the the client library to send this although it does add an authentication cost. |
| 74 | + // HttpResponseMessage response = await graphClient.HttpProvider.SendAsync(req); |
| 75 | + // Since the download URL is preauthenticated, and we aren't deserializing objects, |
| 76 | + // we'd be better to make the request with HttpClient. |
| 77 | + var client = new HttpClient(); |
| 78 | + HttpResponseMessage response = await client.SendAsync(req); |
| 79 | + |
| 80 | + using (Stream responseStream = await response.Content.ReadAsStreamAsync()) |
| 81 | + { |
| 82 | + bytesInStream = new byte[ChunkSize]; |
| 83 | + int read; |
| 84 | + do |
| 85 | + { |
| 86 | + read = responseStream.Read(bytesInStream, 0, (int)bytesInStream.Length); |
| 87 | + if (read > 0) |
| 88 | + fileStream.Write(bytesInStream, 0, bytesInStream.Length); |
| 89 | + } |
| 90 | + while (read > 0); |
| 91 | + } |
| 92 | + offset += ChunkSize + 1; // Move the offset cursor to the next chunk. |
| 93 | + } |
| 94 | + } |
| 95 | + return; |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | + |
| 101 | + |
0 commit comments