Changed SaveAs[Async](string) to CopyTo[Async](Stream)

This commit is contained in:
Kristian Hellang 2016-01-22 09:51:43 +01:00 committed by Chris R
parent 4a3e2ad3c5
commit cb09ffccce
2 changed files with 27 additions and 18 deletions

View File

@ -48,16 +48,16 @@ namespace Microsoft.AspNetCore.Http
Stream OpenReadStream(); Stream OpenReadStream();
/// <summary> /// <summary>
/// Saves the contents of the uploaded file. /// Copies the contents of the uploaded file to the <paramref name="target"/> stream.
/// </summary> /// </summary>
/// <param name="path">The path of the file to create.</param> /// <param name="target">The stream to copy the file contents to.</param>
void SaveAs(string path); void CopyTo(Stream target);
/// <summary> /// <summary>
/// Asynchronously saves the contents of the uploaded file. /// Asynchronously copies the contents of the uploaded file to the <paramref name="target"/> stream.
/// </summary> /// </summary>
/// <param name="path">The path of the file to create.</param> /// <param name="target">The stream to copy the file contents to.</param>
/// <param name="cancellationToken"></param> /// <param name="cancellationToken"></param>
Task SaveAsAsync(string path, CancellationToken cancellationToken = default(CancellationToken)); Task CopyToAsync(Stream target, CancellationToken cancellationToken = default(CancellationToken));
} }
} }

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -72,29 +73,37 @@ namespace Microsoft.AspNetCore.Http.Features.Internal
} }
/// <summary> /// <summary>
/// Saves the contents of the uploaded file. /// Copies the contents of the uploaded file to the <paramref name="target"/> stream.
/// </summary> /// </summary>
/// <param name="path">The path of the file to create.</param> /// <param name="target">The stream to copy the file contents to.</param>
public void SaveAs(string path) public void CopyTo(Stream target)
{ {
using (var fileStream = File.Create(path, DefaultBufferSize)) if (target == null)
{ {
var inputStream = OpenReadStream(); throw new ArgumentNullException(nameof(target));
inputStream.CopyTo(fileStream, DefaultBufferSize); }
using (var readStream = OpenReadStream())
{
readStream.CopyTo(target, DefaultBufferSize);
} }
} }
/// <summary> /// <summary>
/// Asynchronously saves the contents of the uploaded file. /// Asynchronously copies the contents of the uploaded file to the <paramref name="target"/> stream.
/// </summary> /// </summary>
/// <param name="path">The path of the file to create.</param> /// <param name="target">The stream to copy the file contents to.</param>
/// <param name="cancellationToken"></param> /// <param name="cancellationToken"></param>
public async Task SaveAsAsync(string path, CancellationToken cancellationToken = default(CancellationToken)) public async Task CopyToAsync(Stream target, CancellationToken cancellationToken = default(CancellationToken))
{ {
using (var fileStream = File.Create(path, DefaultBufferSize, FileOptions.Asynchronous)) if (target == null)
{ {
var inputStream = OpenReadStream(); throw new ArgumentNullException(nameof(target));
await inputStream.CopyToAsync(fileStream, DefaultBufferSize, cancellationToken); }
using (var readStream = OpenReadStream())
{
await readStream.CopyToAsync(target, DefaultBufferSize, cancellationToken);
} }
} }
} }