Skip to content

Commit 2959292

Browse files
committed
Added more example code to SimpleTest project.
1 parent cac6176 commit 2959292

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

Examples/SimpleTest/Program.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace SimpleTest
22
{
33
using System;
4+
using System.IO;
45
using System.Linq;
56
using System.Net;
67
using System.Net.Http;
@@ -63,6 +64,10 @@ private async Task<int> Run()
6364
{
6465
await Download(path, firstFile.AsFile);
6566
}
67+
68+
await Upload(path, "Test.txt", "This is a text file");
69+
70+
await ChunkUpload(path, "Binary");
6671
}
6772
catch (HttpException e)
6873
{
@@ -253,5 +258,81 @@ private async Task Download(string folder, FileMetadata file)
253258
Console.WriteLine("------------------------------");
254259
}
255260
}
261+
262+
/// <summary>
263+
/// Uploads given content to a file in Dropbox.
264+
/// </summary>
265+
/// <param name="folder">The folder to upload the file.</param>
266+
/// <param name="fileName">The name of the file.</param>
267+
/// <param name="fileContent">The file content.</param>
268+
/// <returns></returns>
269+
private async Task Upload(string folder, string fileName, string fileContent)
270+
{
271+
Console.WriteLine("Upload file...");
272+
273+
using (var stream = new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(fileContent)))
274+
{
275+
var response = await this.client.Files.UploadAsync(folder + "/" + fileName, WriteMode.Overwrite.Instance, body: stream);
276+
277+
Console.WriteLine("Uploaded Id {0} Rev {1}", response.Id, response.Rev);
278+
}
279+
}
280+
281+
/// <summary>
282+
/// Uploads a big file in chunk. The is very helpful for uploading large file in slow network condition
283+
/// and also enable capability to track upload progerss.
284+
/// </summary>
285+
/// <param name="folder">The folder to upload the file.</param>
286+
/// <param name="fileName">The name of the file.</param>
287+
/// <returns></returns>
288+
private async Task ChunkUpload(string folder, string fileName)
289+
{
290+
Console.WriteLine("Chunk upload file...");
291+
// Chunk size is 128KB.
292+
const int chunkSize = 128 * 1024;
293+
294+
// Create a random file of 1MB in size.
295+
var fileContent = new byte[1024 * 1024];
296+
new Random().NextBytes(fileContent);
297+
298+
using (var stream = new MemoryStream(fileContent))
299+
{
300+
int numChunks = (int)Math.Ceiling((double)stream.Length / chunkSize);
301+
302+
byte[] buffer = new byte[chunkSize];
303+
string sessionId = null;
304+
305+
for (var idx = 0; idx < numChunks; idx++)
306+
{
307+
Console.WriteLine("Start uploading chunk {0}", idx);
308+
var byteRead = stream.Read(buffer, 0, chunkSize);
309+
310+
using (MemoryStream memStream = new MemoryStream(buffer, 0, byteRead))
311+
{
312+
if (idx == 0)
313+
{
314+
var result = await this.client.Files.UploadSessionStartAsync(memStream);
315+
sessionId = result.SessionId;
316+
}
317+
318+
else
319+
{
320+
UploadSessionCursor cursor = new UploadSessionCursor(sessionId, (ulong)(chunkSize * idx));
321+
322+
if (idx == numChunks - 1)
323+
{
324+
await this.client.Files.UploadSessionFinishAsync(cursor, new CommitInfo(folder + "/" + fileName), memStream);
325+
}
326+
327+
else
328+
{
329+
await this.client.Files.UploadSessionAppendAsync(cursor, memStream);
330+
}
331+
}
332+
}
333+
}
334+
}
335+
336+
}
256337
}
257338
}

0 commit comments

Comments
 (0)