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

Commit 009cf4a

Browse files
committed
Promote IFormFile extension methods to IFormFile
1 parent fa8c5cb commit 009cf4a

File tree

3 files changed

+91
-62
lines changed

3 files changed

+91
-62
lines changed

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

Lines changed: 37 additions & 1 deletion
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
{
@@ -10,18 +12,52 @@ namespace Microsoft.AspNet.Http
1012
/// </summary>
1113
public interface IFormFile
1214
{
15+
/// <summary>
16+
/// Gets the raw Content-Type header of the uploaded file.
17+
/// </summary>
1318
string ContentType { get; }
1419

20+
/// <summary>
21+
/// Gets the raw Content-Disposition header of the uploaded file.
22+
/// </summary>
1523
string ContentDisposition { get; }
1624

25+
/// <summary>
26+
/// Gets the header dictionary of the uploaded file.
27+
/// </summary>
1728
IHeaderDictionary Headers { get; }
1829

30+
/// <summary>
31+
/// Gets the file length in bytes.
32+
/// </summary>
1933
long Length { get; }
2034

35+
/// <summary>
36+
/// Gets the name from the Content-Disposition header.
37+
/// </summary>
2138
string Name { get; }
2239

40+
/// <summary>
41+
/// Gets the file name from the Content-Disposition header.
42+
/// </summary>
2343
string FileName { get; }
2444

45+
/// <summary>
46+
/// Opens the request stream for reading the uploaded file.
47+
/// </summary>
2548
Stream OpenReadStream();
49+
50+
/// <summary>
51+
/// Saves the contents of the uploaded file.
52+
/// </summary>
53+
/// <param name="path">The path of the file to create.</param>
54+
void SaveAs(string path);
55+
56+
/// <summary>
57+
/// Asynchronously saves the contents of the uploaded file.
58+
/// </summary>
59+
/// <param name="path">The path of the file to create.</param>
60+
/// <param name="cancellationToken"></param>
61+
Task SaveAsAsync(string path, CancellationToken cancellationToken = default(CancellationToken));
2662
}
27-
}
63+
}

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: 54 additions & 1 deletion
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

@@ -20,29 +25,77 @@ public FormFile(Stream baseStream, long baseStreamOffset, long length, string na
2025
FileName = fileName;
2126
}
2227

28+
/// <summary>
29+
/// Gets the raw Content-Disposition header of the uploaded file.
30+
/// </summary>
2331
public string ContentDisposition
2432
{
2533
get { return Headers["Content-Disposition"]; }
2634
set { Headers["Content-Disposition"] = value; }
2735
}
2836

37+
/// <summary>
38+
/// Gets the raw Content-Type header of the uploaded file.
39+
/// </summary>
2940
public string ContentType
3041
{
3142
get { return Headers["Content-Type"]; }
3243
set { Headers["Content-Type"] = value; }
3344
}
3445

46+
/// <summary>
47+
/// Gets the header dictionary of the uploaded file.
48+
/// </summary>
3549
public IHeaderDictionary Headers { get; set; }
3650

51+
/// <summary>
52+
/// Gets the file length in bytes.
53+
/// </summary>
3754
public long Length { get; }
3855

56+
/// <summary>
57+
/// Gets the name from the Content-Disposition header.
58+
/// </summary>
3959
public string Name { get; }
4060

61+
/// <summary>
62+
/// Gets the file name from the Content-Disposition header.
63+
/// </summary>
4164
public string FileName { get; }
4265

66+
/// <summary>
67+
/// Opens the request stream for reading the uploaded file.
68+
/// </summary>
4369
public Stream OpenReadStream()
4470
{
4571
return new ReferenceReadStream(_baseStream, _baseStreamOffset, Length);
4672
}
73+
74+
/// <summary>
75+
/// Saves the contents of the uploaded file.
76+
/// </summary>
77+
/// <param name="path">The path of the file to create.</param>
78+
public void SaveAs(string path)
79+
{
80+
using (var fileStream = File.Create(path, DefaultBufferSize))
81+
{
82+
var inputStream = OpenReadStream();
83+
inputStream.CopyTo(fileStream, DefaultBufferSize);
84+
}
85+
}
86+
87+
/// <summary>
88+
/// Asynchronously saves the contents of the uploaded file.
89+
/// </summary>
90+
/// <param name="path">The path of the file to create.</param>
91+
/// <param name="cancellationToken"></param>
92+
public async Task SaveAsAsync(string path, CancellationToken cancellationToken = default(CancellationToken))
93+
{
94+
using (var fileStream = File.Create(path, DefaultBufferSize))
95+
{
96+
var inputStream = OpenReadStream();
97+
await inputStream.CopyToAsync(fileStream, DefaultBufferSize, cancellationToken);
98+
}
99+
}
47100
}
48-
}
101+
}

0 commit comments

Comments
 (0)