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

Commit cc537ef

Browse files
committed
Moved FormFileExtensions methods to IFormFile
1 parent fa8c5cb commit cc537ef

File tree

3 files changed

+62
-60
lines changed

3 files changed

+62
-60
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System.IO;
5+
using System.Threading;
6+
using System.Threading.Tasks;
57

68
namespace Microsoft.AspNet.Http
79
{
@@ -23,5 +25,24 @@ public interface IFormFile
2325
string FileName { get; }
2426

2527
Stream OpenReadStream();
28+
29+
/// <summary>
30+
/// Saves the contents of the uploaded file.
31+
/// </summary>
32+
/// <param name="path">The path of the file to create.</param>
33+
void SaveAs(string path);
34+
35+
/// <summary>
36+
/// Asynchronously saves the contents of the uploaded file.
37+
/// </summary>
38+
/// <param name="path">The path of the file to create.</param>
39+
Task SaveAsAsync(string path);
40+
41+
/// <summary>
42+
/// Asynchronously saves the contents of the uploaded file.
43+
/// </summary>
44+
/// <param name="path">The path of the file to create.</param>
45+
/// <param name="cancellationToken"></param>
46+
Task SaveAsAsync(string path, CancellationToken cancellationToken);
2647
}
2748
}

src/Microsoft.AspNet.Http.Extensions/FormFileExtensions.cs

Lines changed: 0 additions & 60 deletions
This file was deleted.

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System.IO;
5+
using System.Threading;
6+
using System.Threading.Tasks;
57
using Microsoft.AspNet.Http.Internal;
68

79
namespace Microsoft.AspNet.Http.Features.Internal
810
{
911
public class FormFile : IFormFile
1012
{
13+
// Stream.CopyTo method uses 80KB as the default buffer size.
14+
private const int DefaultBufferSize = 80 * 1024;
15+
1116
private readonly Stream _baseStream;
1217
private readonly long _baseStreamOffset;
1318

@@ -44,5 +49,41 @@ public Stream OpenReadStream()
4449
{
4550
return new ReferenceReadStream(_baseStream, _baseStreamOffset, Length);
4651
}
52+
53+
/// <summary>
54+
/// Saves the contents of the uploaded file.
55+
/// </summary>
56+
/// <param name="path">The path of the file to create.</param>
57+
public void SaveAs(string path)
58+
{
59+
using (var fileStream = new FileStream(path, FileMode.Create))
60+
{
61+
var inputStream = OpenReadStream();
62+
inputStream.CopyTo(fileStream);
63+
}
64+
}
65+
66+
/// <summary>
67+
/// Asynchronously saves the contents of the uploaded file.
68+
/// </summary>
69+
/// <param name="path">The path of the file to create.</param>
70+
public Task SaveAsAsync(string path)
71+
{
72+
return SaveAsAsync(path, CancellationToken.None);
73+
}
74+
75+
/// <summary>
76+
/// Asynchronously saves the contents of the uploaded file.
77+
/// </summary>
78+
/// <param name="path">The path of the file to create.</param>
79+
/// <param name="cancellationToken"></param>
80+
public async Task SaveAsAsync(string path, CancellationToken cancellationToken)
81+
{
82+
using (var fileStream = new FileStream(path, FileMode.Create))
83+
{
84+
var inputStream = OpenReadStream();
85+
await inputStream.CopyToAsync(fileStream, DefaultBufferSize, cancellationToken);
86+
}
87+
}
4788
}
4889
}

0 commit comments

Comments
 (0)