public async Task UploadChunkTest(string path, string controller = Constants.UploadChunk, int bufferSize = DefaultBufferSize) { var transactionId = Guid.NewGuid(); var chunkId = 0; using (var stream = File.OpenRead(path)) using (var bufferedStream = new ProgressStream(stream, Progress, bufferSize)) { int length; var buffer = new byte[bufferSize]; while ((length = await bufferedStream.ReadAsync(buffer, 0, bufferSize)) > 0) { var url = $"{Constants.FilesRoute}/{controller}/{transactionId}"; var last = stream.Position >= stream.Length; if (controller == Constants.UploadChunk) { url += $"/{chunkId++}/{last}"; } var result = await Client.PostAsync( url, new ByteArrayContent(buffer, 0, length)); Assert.True(result.IsSuccessStatusCode); if (last && controller == Constants.UploadChunk) { var resultLength = await result.Content.ReadAsAsync <long>(); Assert.Equal(stream.Length, resultLength); } } } }
public async Task UploadByChunks(string path) { var current = 0; var bufferSize = 1024 * 1000 * 16; //16MB chunks var notifications = 0; var progress = new Progress <decimal>(d => notifications++); using (var stream = File.OpenRead(path)) using (var bufferedStream = new ProgressStream(stream, progress, bufferSize)) { int length; var buffer = new byte[bufferSize]; while ((length = await bufferedStream.ReadAsync(buffer, 0, bufferSize)) > 0) { var result = await Client.PostAsync($"{Constants.FilesRoute}/{Constants.HugeFile}/{current}", new ByteArrayContent(buffer, 0, length)); Assert.True(result.IsSuccessStatusCode); } } }