2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System . IO ;
5
+ using System . Threading ;
6
+ using System . Threading . Tasks ;
5
7
using Microsoft . AspNet . Http . Internal ;
6
8
7
9
namespace Microsoft . AspNet . Http . Features . Internal
8
10
{
9
11
public class FormFile : IFormFile
10
12
{
13
+ // Stream.CopyTo method uses 80KB as the default buffer size.
14
+ private const int DefaultBufferSize = 80 * 1024 ;
15
+
11
16
private readonly Stream _baseStream ;
12
17
private readonly long _baseStreamOffset ;
13
18
@@ -20,29 +25,77 @@ public FormFile(Stream baseStream, long baseStreamOffset, long length, string na
20
25
FileName = fileName ;
21
26
}
22
27
28
+ /// <summary>
29
+ /// Gets the raw Content-Disposition header of the uploaded file.
30
+ /// </summary>
23
31
public string ContentDisposition
24
32
{
25
33
get { return Headers [ "Content-Disposition" ] ; }
26
34
set { Headers [ "Content-Disposition" ] = value ; }
27
35
}
28
36
37
+ /// <summary>
38
+ /// Gets the raw Content-Type header of the uploaded file.
39
+ /// </summary>
29
40
public string ContentType
30
41
{
31
42
get { return Headers [ "Content-Type" ] ; }
32
43
set { Headers [ "Content-Type" ] = value ; }
33
44
}
34
45
46
+ /// <summary>
47
+ /// Gets the header dictionary of the uploaded file.
48
+ /// </summary>
35
49
public IHeaderDictionary Headers { get ; set ; }
36
50
51
+ /// <summary>
52
+ /// Gets the file length in bytes.
53
+ /// </summary>
37
54
public long Length { get ; }
38
55
56
+ /// <summary>
57
+ /// Gets the name from the Content-Disposition header.
58
+ /// </summary>
39
59
public string Name { get ; }
40
60
61
+ /// <summary>
62
+ /// Gets the file name from the Content-Disposition header.
63
+ /// </summary>
41
64
public string FileName { get ; }
42
65
66
+ /// <summary>
67
+ /// Opens the request stream for reading the uploaded file.
68
+ /// </summary>
43
69
public Stream OpenReadStream ( )
44
70
{
45
71
return new ReferenceReadStream ( _baseStream , _baseStreamOffset , Length ) ;
46
72
}
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
+ }
47
100
}
48
- }
101
+ }
0 commit comments