diff --git a/src/Microsoft.AspNet.Http.Abstractions/IFormFile.cs b/src/Microsoft.AspNet.Http.Abstractions/IFormFile.cs index b5e44904..eca4d6a2 100644 --- a/src/Microsoft.AspNet.Http.Abstractions/IFormFile.cs +++ b/src/Microsoft.AspNet.Http.Abstractions/IFormFile.cs @@ -2,6 +2,8 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.IO; +using System.Threading; +using System.Threading.Tasks; namespace Microsoft.AspNet.Http { @@ -10,18 +12,52 @@ namespace Microsoft.AspNet.Http /// public interface IFormFile { + /// + /// Gets the raw Content-Type header of the uploaded file. + /// string ContentType { get; } + /// + /// Gets the raw Content-Disposition header of the uploaded file. + /// string ContentDisposition { get; } + /// + /// Gets the header dictionary of the uploaded file. + /// IHeaderDictionary Headers { get; } + /// + /// Gets the file length in bytes. + /// long Length { get; } + /// + /// Gets the name from the Content-Disposition header. + /// string Name { get; } + /// + /// Gets the file name from the Content-Disposition header. + /// string FileName { get; } + /// + /// Opens the request stream for reading the uploaded file. + /// Stream OpenReadStream(); + + /// + /// Saves the contents of the uploaded file. + /// + /// The path of the file to create. + void SaveAs(string path); + + /// + /// Asynchronously saves the contents of the uploaded file. + /// + /// The path of the file to create. + /// + Task SaveAsAsync(string path, CancellationToken cancellationToken = default(CancellationToken)); } -} \ No newline at end of file +} diff --git a/src/Microsoft.AspNet.Http.Extensions/FormFileExtensions.cs b/src/Microsoft.AspNet.Http.Extensions/FormFileExtensions.cs deleted file mode 100644 index fa68ae34..00000000 --- a/src/Microsoft.AspNet.Http.Extensions/FormFileExtensions.cs +++ /dev/null @@ -1,60 +0,0 @@ -// 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; - -namespace Microsoft.AspNet.Http -{ - /// - /// Extension methods for . - /// - public static class FormFileExtensions - { - // Stream.CopyTo method uses 80KB as the default buffer size. - private static int DefaultBufferSize = 80 * 1024; - - /// - /// Saves the contents of an uploaded file. - /// - /// The . - /// The name of the file to create. - public static void SaveAs(this IFormFile formFile, string filename) - { - if (formFile == null) - { - throw new ArgumentNullException(nameof(formFile)); - } - - using (var fileStream = new FileStream(filename, FileMode.Create)) - { - var inputStream = formFile.OpenReadStream(); - inputStream.CopyTo(fileStream); - } - } - - /// - /// Asynchronously saves the contents of an uploaded file. - /// - /// The . - /// The name of the file to create. - public async static Task SaveAsAsync( - this IFormFile formFile, - string filename, - CancellationToken cancellationToken = default(CancellationToken)) - { - if (formFile == null) - { - throw new ArgumentNullException(nameof(formFile)); - } - - using (var fileStream = new FileStream(filename, FileMode.Create)) - { - var inputStream = formFile.OpenReadStream(); - await inputStream.CopyToAsync(fileStream, DefaultBufferSize, cancellationToken); - } - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Http/Features/FormFile.cs b/src/Microsoft.AspNet.Http/Features/FormFile.cs index 3fc51136..f9e4bfc5 100644 --- a/src/Microsoft.AspNet.Http/Features/FormFile.cs +++ b/src/Microsoft.AspNet.Http/Features/FormFile.cs @@ -2,12 +2,17 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.IO; +using System.Threading; +using System.Threading.Tasks; using Microsoft.AspNet.Http.Internal; namespace Microsoft.AspNet.Http.Features.Internal { public class FormFile : IFormFile { + // Stream.CopyTo method uses 80KB as the default buffer size. + private const int DefaultBufferSize = 80 * 1024; + private readonly Stream _baseStream; private readonly long _baseStreamOffset; @@ -20,29 +25,77 @@ public FormFile(Stream baseStream, long baseStreamOffset, long length, string na FileName = fileName; } + /// + /// Gets the raw Content-Disposition header of the uploaded file. + /// public string ContentDisposition { get { return Headers["Content-Disposition"]; } set { Headers["Content-Disposition"] = value; } } + /// + /// Gets the raw Content-Type header of the uploaded file. + /// public string ContentType { get { return Headers["Content-Type"]; } set { Headers["Content-Type"] = value; } } + /// + /// Gets the header dictionary of the uploaded file. + /// public IHeaderDictionary Headers { get; set; } + /// + /// Gets the file length in bytes. + /// public long Length { get; } + /// + /// Gets the name from the Content-Disposition header. + /// public string Name { get; } + /// + /// Gets the file name from the Content-Disposition header. + /// public string FileName { get; } + /// + /// Opens the request stream for reading the uploaded file. + /// public Stream OpenReadStream() { return new ReferenceReadStream(_baseStream, _baseStreamOffset, Length); } + + /// + /// Saves the contents of the uploaded file. + /// + /// The path of the file to create. + public void SaveAs(string path) + { + using (var fileStream = File.Create(path, DefaultBufferSize)) + { + var inputStream = OpenReadStream(); + inputStream.CopyTo(fileStream, DefaultBufferSize); + } + } + + /// + /// Asynchronously saves the contents of the uploaded file. + /// + /// The path of the file to create. + /// + public async Task SaveAsAsync(string path, CancellationToken cancellationToken = default(CancellationToken)) + { + using (var fileStream = File.Create(path, DefaultBufferSize, FileOptions.Asynchronous)) + { + var inputStream = OpenReadStream(); + await inputStream.CopyToAsync(fileStream, DefaultBufferSize, cancellationToken); + } + } } -} \ No newline at end of file +}