1
- using Microsoft . AspNetCore . Hosting ;
2
- using Microsoft . AspNetCore . Builder ;
3
- using Microsoft . AspNetCore . Http ;
4
- using Microsoft . AspNetCore . Routing ;
5
- using System . IO ;
6
- using Microsoft . Extensions . Hosting ;
1
+ var app = WebApplication . Create ( ) ;
7
2
8
- namespace PracticalAspNetCore
3
+ app . MapGet ( "" , async context =>
9
4
{
10
- public class Startup
11
- {
12
- public void Configure ( IApplicationBuilder app , IHostEnvironment env )
13
- {
14
- app . UseRouting ( ) ;
15
- app . UseEndpoints ( endpoints =>
16
- {
17
- endpoints . MapGet ( "" , async context =>
18
- {
19
- context . Response . Headers . Add ( "content-type" , "text/html" ) ;
5
+ context . Response . Headers . Add ( "content-type" , "text/html" ) ;
20
6
21
- var body = $@ "
7
+ var body = $@ "
8
+ <html><body>
22
9
<h1>Upload File</h1>
23
10
<form action=""Upload"" method=""post"" enctype=""multipart/form-data"">
24
11
<input type=""file"" name=""file"" />
25
12
<input type=""submit"" value=""Upload"" />
26
13
</form>
14
+ </body></html>
27
15
" ;
28
16
29
- await context . Response . WriteAsync ( body ) ;
30
- } ) ;
17
+ await context . Response . WriteAsync ( body ) ;
18
+ } ) ;
31
19
32
- endpoints . MapPost ( "Upload" , async context =>
33
- {
34
- if ( context . Request . HasFormContentType )
35
- {
36
- var form = await context . Request . ReadFormAsync ( ) ;
37
-
38
- foreach ( var f in form . Files )
39
- {
40
- using ( var body = f . OpenReadStream ( ) )
41
- {
42
- var fileName = Path . Combine ( env . ContentRootPath , f . FileName ) ;
43
- File . WriteAllBytes ( fileName , ReadFully ( body ) ) ;
44
- await context . Response . WriteAsync ( $ "Uploaded file written to { fileName } ") ;
45
- }
46
- }
47
- }
48
- await context . Response . WriteAsync ( "" ) ;
49
- } ) ;
50
- } ) ;
51
- }
20
+ app . MapPost ( "Upload" , async context =>
21
+ {
22
+ if ( context . Request . HasFormContentType )
23
+ {
24
+ var form = await context . Request . ReadFormAsync ( ) ;
52
25
53
- public static byte [ ] ReadFully ( Stream input )
26
+ foreach ( var f in form . Files )
54
27
{
55
- byte [ ] buffer = new byte [ 16 * 1024 ] ;
56
- using var ms = new MemoryStream ( ) ;
57
- int read ;
58
- while ( ( read = input . Read ( buffer , 0 , buffer . Length ) ) > 0 )
28
+ using ( var body = f . OpenReadStream ( ) )
59
29
{
60
- ms . Write ( buffer , 0 , read ) ;
30
+ var fileName = Path . Combine ( app . Environment . ContentRootPath , f . FileName ) ;
31
+ File . WriteAllBytes ( fileName , ReadFully ( body ) ) ;
32
+ await context . Response . WriteAsync ( $ "Uploaded file written to { fileName } ") ;
61
33
}
62
- return ms . ToArray ( ) ;
63
34
}
64
35
}
36
+ await context . Response . WriteAsync ( "" ) ;
37
+ } ) ;
65
38
39
+ app . Run ( ) ;
66
40
41
+ static byte [ ] ReadFully ( Stream input )
42
+ {
43
+ byte [ ] buffer = new byte [ 16 * 1024 ] ;
44
+ using var ms = new MemoryStream ( ) ;
45
+ int read ;
46
+ while ( ( read = input . Read ( buffer , 0 , buffer . Length ) ) > 0 )
47
+ {
48
+ ms . Write ( buffer , 0 , read ) ;
49
+ }
50
+ return ms . ToArray ( ) ;
67
51
}
0 commit comments