Skip to content

Commit d8820f0

Browse files
committed
Upgrade to .NET 6
1 parent e9a1122 commit d8820f0

File tree

2 files changed

+32
-51
lines changed

2 files changed

+32
-51
lines changed
+31-47
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,51 @@
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();
72

8-
namespace PracticalAspNetCore
3+
app.MapGet("", async context =>
94
{
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");
206

21-
var body = $@"
7+
var body = $@"
8+
<html><body>
229
<h1>Upload File</h1>
2310
<form action=""Upload"" method=""post"" enctype=""multipart/form-data"">
2411
<input type=""file"" name=""file"" />
2512
<input type=""submit"" value=""Upload"" />
2613
</form>
14+
</body></html>
2715
";
2816

29-
await context.Response.WriteAsync(body);
30-
});
17+
await context.Response.WriteAsync(body);
18+
});
3119

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();
5225

53-
public static byte[] ReadFully(Stream input)
26+
foreach (var f in form.Files)
5427
{
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())
5929
{
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}");
6133
}
62-
return ms.ToArray();
6334
}
6435
}
36+
await context.Response.WriteAsync("");
37+
});
6538

39+
app.Run();
6640

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();
6751
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22
<PropertyGroup>
33
<TargetFramework>net6.0</TargetFramework>
4-
<AssemblyName>form-upload-file</AssemblyName>
5-
6-
<PackageId>form-upload-file</PackageId>
7-
<ImplicitUsings>true</ImplicitUsings>
4+
<ImplicitUsings>true</ImplicitUsings>
85
</PropertyGroup>
96
</Project>

0 commit comments

Comments
 (0)