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

Commit 0f05427

Browse files
committed
Changed SaveAs(string) to Save(Stream)
1 parent da478b0 commit 0f05427

File tree

3 files changed

+52
-18
lines changed

3 files changed

+52
-18
lines changed

src/Microsoft.AspNet.Http.Abstractions/IFormFile.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ public interface IFormFile
5050
/// <summary>
5151
/// Saves the contents of the uploaded file.
5252
/// </summary>
53-
/// <param name="path">The path of the file to create.</param>
54-
void SaveAs(string path);
53+
/// <param name="target">The stream to save the file to.</param>
54+
void Save(Stream target);
5555

5656
/// <summary>
5757
/// Asynchronously saves the contents of the uploaded file.
5858
/// </summary>
59-
/// <param name="path">The path of the file to create.</param>
59+
/// <param name="target">The stream to save the file to.</param>
6060
/// <param name="cancellationToken"></param>
61-
Task SaveAsAsync(string path, CancellationToken cancellationToken = default(CancellationToken));
61+
Task SaveAsync(Stream target, CancellationToken cancellationToken = default(CancellationToken));
6262
}
6363
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.IO;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
8+
namespace Microsoft.AspNet.Http.Extensions
9+
{
10+
public static class FormFileExtensions
11+
{
12+
// Stream.CopyTo method uses 80KB as the default buffer size.
13+
private const int DefaultBufferSize = 80 * 1024;
14+
15+
/// <summary>
16+
/// Saves the contents of the uploaded file.
17+
/// </summary>
18+
/// <param name="file">The file to save.</param>
19+
/// <param name="path">The path of the file to create.</param>
20+
public static void SaveAs(this IFormFile file, string path)
21+
{
22+
using (var fileStream = File.Create(path, DefaultBufferSize))
23+
{
24+
file.Save(fileStream);
25+
}
26+
}
27+
28+
/// <summary>
29+
/// Asynchronously saves the contents of the uploaded file.
30+
/// </summary>
31+
/// <param name="file">The file to save.</param>
32+
/// <param name="path">The path of the file to create.</param>
33+
/// <param name="cancellationToken"></param>
34+
public static async Task SaveAsAsync(this IFormFile file, string path, CancellationToken cancellationToken = default(CancellationToken))
35+
{
36+
using (var fileStream = File.Create(path, DefaultBufferSize, FileOptions.Asynchronous))
37+
{
38+
await file.SaveAsync(fileStream, cancellationToken);
39+
}
40+
}
41+
}
42+
}

src/Microsoft.AspNet.Http/Features/FormFile.cs

+6-14
Original file line numberDiff line numberDiff line change
@@ -74,28 +74,20 @@ public Stream OpenReadStream()
7474
/// <summary>
7575
/// Saves the contents of the uploaded file.
7676
/// </summary>
77-
/// <param name="path">The path of the file to create.</param>
78-
public void SaveAs(string path)
77+
/// <param name="target">The stream to save the file to.</param>
78+
public void Save(Stream target)
7979
{
80-
using (var fileStream = File.Create(path, DefaultBufferSize))
81-
{
82-
var inputStream = OpenReadStream();
83-
inputStream.CopyTo(fileStream, DefaultBufferSize);
84-
}
80+
OpenReadStream().CopyTo(target, DefaultBufferSize);
8581
}
8682

8783
/// <summary>
8884
/// Asynchronously saves the contents of the uploaded file.
8985
/// </summary>
90-
/// <param name="path">The path of the file to create.</param>
86+
/// <param name="target">The stream to save the file to.</param>
9187
/// <param name="cancellationToken"></param>
92-
public async Task SaveAsAsync(string path, CancellationToken cancellationToken = default(CancellationToken))
88+
public async Task SaveAsync(Stream target, CancellationToken cancellationToken = default(CancellationToken))
9389
{
94-
using (var fileStream = File.Create(path, DefaultBufferSize, FileOptions.Asynchronous))
95-
{
96-
var inputStream = OpenReadStream();
97-
await inputStream.CopyToAsync(fileStream, DefaultBufferSize, cancellationToken);
98-
}
90+
await OpenReadStream().CopyToAsync(target, DefaultBufferSize, cancellationToken);
9991
}
10092
}
10193
}

0 commit comments

Comments
 (0)