Added extension methods for FormFile

This commit is contained in:
Ajay Bhargav Baaskaran 2015-01-09 12:53:23 -08:00
parent 5872feb224
commit 4377bb24ce
1 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,47 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// 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
{
/// <summary>
/// Extension methods for <see cref="IFormFile"/>.
/// </summary>
public static class FormFileExtensions
{
private static int DefaultBufferSize = 81920;
/// <summary>
/// Saves the contents of an uploaded file.
/// </summary>
/// <param name="formFile">The <see cref="IFormFile"/>.</param>
/// <param name="filename">The name of the file to create.</param>
public static void SaveAs([NotNull] this IFormFile formFile, string filename)
{
using (var fileStream = new FileStream(filename, FileMode.Create))
{
var inputStream = formFile.OpenReadStream();
inputStream.CopyTo(fileStream);
}
}
/// <summary>
/// Asynchronously saves the contents of an uploaded file.
/// </summary>
/// <param name="formFile">The <see cref="IFormFile"/>.</param>
/// <param name="filename">The name of the file to create.</param>
public async static Task SaveAsAsync([NotNull] this IFormFile formFile,
string filename,
CancellationToken cancellationToken = default(CancellationToken))
{
using (var fileStream = new FileStream(filename, FileMode.Create))
{
var inputStream = formFile.OpenReadStream();
await inputStream.CopyToAsync(fileStream, DefaultBufferSize, cancellationToken);
}
}
}
}