Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/Microsoft.AspNetCore.Http.Abstractions/IFormFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ public interface IFormFile
Stream OpenReadStream();

/// <summary>
/// Saves the contents of the uploaded file.
/// Copies the contents of the uploaded file to the <paramref name="target"/> stream.
/// </summary>
/// <param name="path">The path of the file to create.</param>
void SaveAs(string path);
/// <param name="target">The stream to copy the file contents to.</param>
void CopyTo(Stream target);

/// <summary>
/// Asynchronously saves the contents of the uploaded file.
/// Asynchronously copies the contents of the uploaded file to the <paramref name="target"/> stream.
/// </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>
Task SaveAsAsync(string path, CancellationToken cancellationToken = default(CancellationToken));
Task CopyToAsync(Stream target, CancellationToken cancellationToken = default(CancellationToken));
}
}
33 changes: 21 additions & 12 deletions src/Microsoft.AspNetCore.Http/Features/FormFile.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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.

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -72,29 +73,37 @@ public Stream OpenReadStream()
}

/// <summary>
/// Saves the contents of the uploaded file.
/// Copies the contents of the uploaded file to the <paramref name="target"/> stream.
/// </summary>
/// <param name="path">The path of the file to create.</param>
public void SaveAs(string path)
/// <param name="target">The stream to copy the file contents to.</param>
public void CopyTo(Stream target)
{
using (var fileStream = File.Create(path, DefaultBufferSize))
if (target == null)
{
var inputStream = OpenReadStream();
inputStream.CopyTo(fileStream, DefaultBufferSize);
throw new ArgumentNullException(nameof(target));
}

using (var readStream = OpenReadStream())
{
readStream.CopyTo(target, DefaultBufferSize);
}
}

/// <summary>
/// Asynchronously saves the contents of the uploaded file.
/// Asynchronously copies the contents of the uploaded file to the <paramref name="target"/> stream.
/// </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>
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)
{
throw new ArgumentNullException(nameof(target));
}

using (var readStream = OpenReadStream())
{
var inputStream = OpenReadStream();
await inputStream.CopyToAsync(fileStream, DefaultBufferSize, cancellationToken);
await readStream.CopyToAsync(target, DefaultBufferSize, cancellationToken);
}
}
}
Expand Down