- Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathProgram.cs
51 lines (44 loc) · 1.4 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
varapp=WebApplication.Create();
app.MapGet("",async context =>
{
context.Response.Headers.Append("content-type","text/html");
varbody=$@"
<html><body>
<h1>Upload File</h1>
<form action=""Upload"" method=""post"" enctype=""multipart/form-data"">
<input type=""file"" name=""file"" />
<input type=""submit"" value=""Upload"" />
</form>
</body></html>
";
awaitcontext.Response.WriteAsync(body);
});
app.MapPost("Upload",async context =>
{
if(context.Request.HasFormContentType)
{
varform=awaitcontext.Request.ReadFormAsync();
foreach(varfinform.Files)
{
using(varbody=f.OpenReadStream())
{
varfileName=Path.Combine(app.Environment.ContentRootPath,f.FileName);
File.WriteAllBytes(fileName,ReadFully(body));
awaitcontext.Response.WriteAsync($"Uploaded file written to {fileName}");
}
}
}
awaitcontext.Response.WriteAsync("");
});
app.Run();
staticbyte[]ReadFully(Streaminput)
{
byte[]buffer=newbyte[16*1024];
usingvarms=newMemoryStream();
intread;
while((read=input.Read(buffer,0,buffer.Length))>0)
{
ms.Write(buffer,0,read);
}
returnms.ToArray();
}