diff --git a/src/Microsoft.AspNetCore.Http.Abstractions/IFormFile.cs b/src/Microsoft.AspNetCore.Http.Abstractions/IFormFile.cs index 871ca0ef..7cb569d7 100644 --- a/src/Microsoft.AspNetCore.Http.Abstractions/IFormFile.cs +++ b/src/Microsoft.AspNetCore.Http.Abstractions/IFormFile.cs @@ -48,16 +48,16 @@ public interface IFormFile Stream OpenReadStream(); /// - /// Saves the contents of the uploaded file. + /// Copies the contents of the uploaded file to the stream. /// - /// The path of the file to create. - void SaveAs(string path); + /// The stream to copy the file contents to. + void CopyTo(Stream target); /// - /// Asynchronously saves the contents of the uploaded file. + /// Asynchronously copies the contents of the uploaded file to the stream. /// - /// The path of the file to create. + /// The stream to copy the file contents to. /// - Task SaveAsAsync(string path, CancellationToken cancellationToken = default(CancellationToken)); + Task CopyToAsync(Stream target, CancellationToken cancellationToken = default(CancellationToken)); } } diff --git a/src/Microsoft.AspNetCore.Http/Features/FormFile.cs b/src/Microsoft.AspNetCore.Http/Features/FormFile.cs index 72335cdb..3a7b3415 100644 --- a/src/Microsoft.AspNetCore.Http/Features/FormFile.cs +++ b/src/Microsoft.AspNetCore.Http/Features/FormFile.cs @@ -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; @@ -72,29 +73,37 @@ public Stream OpenReadStream() } /// - /// Saves the contents of the uploaded file. + /// Copies the contents of the uploaded file to the stream. /// - /// The path of the file to create. - public void SaveAs(string path) + /// The stream to copy the file contents to. + 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); } } /// - /// Asynchronously saves the contents of the uploaded file. + /// Asynchronously copies the contents of the uploaded file to the stream. /// - /// The path of the file to create. + /// The stream to copy the file contents to. /// - 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); } } }