From 9fcbf013dd4d4c5c6578045e6c6ae63a0e5e5818 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Fri, 15 Jul 2022 04:08:12 +0100 Subject: [PATCH 1/8] created project --- .../minimal-apis/samples/IFormFile/IFormFile.csproj | 9 +++++++++ .../minimal-apis/samples/IFormFile/Program.cs | 6 ++++++ .../samples/IFormFile/appsettings.Development.json | 8 ++++++++ .../minimal-apis/samples/IFormFile/appsettings.json | 9 +++++++++ 4 files changed, 32 insertions(+) create mode 100644 aspnetcore/fundamentals/minimal-apis/samples/IFormFile/IFormFile.csproj create mode 100644 aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs create mode 100644 aspnetcore/fundamentals/minimal-apis/samples/IFormFile/appsettings.Development.json create mode 100644 aspnetcore/fundamentals/minimal-apis/samples/IFormFile/appsettings.json diff --git a/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/IFormFile.csproj b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/IFormFile.csproj new file mode 100644 index 000000000000..4c2bb77d0106 --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/IFormFile.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + enable + enable + + + diff --git a/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs new file mode 100644 index 000000000000..1760df1d28be --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs @@ -0,0 +1,6 @@ +var builder = WebApplication.CreateBuilder(args); +var app = builder.Build(); + +app.MapGet("/", () => "Hello World!"); + +app.Run(); diff --git a/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/appsettings.Development.json b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/appsettings.Development.json new file mode 100644 index 000000000000..0c208ae9181e --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/appsettings.json b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/appsettings.json new file mode 100644 index 000000000000..10f68b8c8b4f --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} From 13825beeb09dca623eefaf3849c659b93ee7d587 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Fri, 15 Jul 2022 04:09:57 +0100 Subject: [PATCH 2/8] added endpoints --- .../minimal-apis/samples/IFormFile/Program.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs index 1760df1d28be..e17d0574a972 100644 --- a/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs @@ -3,4 +3,21 @@ app.MapGet("/", () => "Hello World!"); -app.Run(); +app.MapPost("/fileUp", async (IFormFile file) => +{ + string tempfile = Path.GetTempFileName(); + using var stream = File.OpenWrite(tempfile); + await file.CopyToAsync(stream); +}); + +app.MapPost("/FilesUp", async (IFormFileCollection myFiles) => +{ + foreach (var file in myFiles) + { + string tempfile = Path.GetTempFileName(); + using var stream = File.OpenWrite(tempfile); + await file.CopyToAsync(stream); + } +}); + +app.Run(); \ No newline at end of file From 17e329ba526ac736df753745a14367abc5fa1a21 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Fri, 15 Jul 2022 20:35:13 +0100 Subject: [PATCH 3/8] added some lines --- .../minimal-apis/samples/IFormFile/Program.cs | 9 ++++++++- .../minimal-apis/samples/IFormFile/index.html | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 aspnetcore/fundamentals/minimal-apis/samples/IFormFile/index.html diff --git a/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs index e17d0574a972..dd1529370a6b 100644 --- a/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs @@ -1,6 +1,12 @@ var builder = WebApplication.CreateBuilder(args); +//add cors +builder.Services.AddCors(option=>{ + option.AddPolicy("AllowAll", p=>p.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()); + option.AddDefaultPolicy(p=>p.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()); +}); var app = builder.Build(); - +//add cors +app.UseCors("AllowAll"); app.MapGet("/", () => "Hello World!"); app.MapPost("/fileUp", async (IFormFile file) => @@ -8,6 +14,7 @@ string tempfile = Path.GetTempFileName(); using var stream = File.OpenWrite(tempfile); await file.CopyToAsync(stream); + return TypedResults.Redirect("/"); }); app.MapPost("/FilesUp", async (IFormFileCollection myFiles) => diff --git a/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/index.html b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/index.html new file mode 100644 index 000000000000..c72e104b3a57 --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/index.html @@ -0,0 +1,14 @@ + + + + + + JS Bin + + +
+ Swac + +
+ + \ No newline at end of file From 191a99e1994600d203c2cbcaa1751e8531b96327 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Sat, 16 Jul 2022 06:22:46 +0100 Subject: [PATCH 4/8] chnaged url --- .../fundamentals/minimal-apis/samples/IFormFile/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/index.html b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/index.html index c72e104b3a57..364248614ea3 100644 --- a/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/index.html +++ b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/index.html @@ -6,7 +6,7 @@ JS Bin -
+ Swac
From f7d64965b37704351c595aec6220543bdb135180 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Sat, 16 Jul 2022 06:31:56 +0100 Subject: [PATCH 5/8] added name attribute --- .../fundamentals/minimal-apis/samples/IFormFile/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/index.html b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/index.html index 364248614ea3..4748bf1e7d1c 100644 --- a/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/index.html +++ b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/index.html @@ -7,7 +7,7 @@
- Swac + Swac
From 84c87473fbb0766209bc03e5781ed835ab0831fb Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Sat, 16 Jul 2022 16:13:41 +0100 Subject: [PATCH 6/8] added sample html pages for single and multiple uploads --- .../minimal-apis/samples/IFormFile/Program.cs | 20 +++++++++---------- .../minimal-apis/samples/IFormFile/index.html | 14 ------------- 2 files changed, 9 insertions(+), 25 deletions(-) delete mode 100644 aspnetcore/fundamentals/minimal-apis/samples/IFormFile/index.html diff --git a/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs index dd1529370a6b..5bce0d8c4cb2 100644 --- a/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs @@ -1,30 +1,28 @@ var builder = WebApplication.CreateBuilder(args); -//add cors -builder.Services.AddCors(option=>{ - option.AddPolicy("AllowAll", p=>p.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()); - option.AddDefaultPolicy(p=>p.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()); -}); + var app = builder.Build(); -//add cors -app.UseCors("AllowAll"); -app.MapGet("/", () => "Hello World!"); -app.MapPost("/fileUp", async (IFormFile file) => +app.MapGet("/singleFile", () => Results.Text(@"
Swac
", "text/html")); +app.MapGet("/multipleFiles", () => Results.Text(@"
Swac
", "text/html")); +app.MapPost("/fileup", async (IFormFile file) => { + // save a file string tempfile = Path.GetTempFileName(); using var stream = File.OpenWrite(tempfile); await file.CopyToAsync(stream); - return TypedResults.Redirect("/"); + return Results.Accepted(); }); -app.MapPost("/FilesUp", async (IFormFileCollection myFiles) => +app.MapPost("/filesup", async (IFormFileCollection myFiles) => { + // save file collections foreach (var file in myFiles) { string tempfile = Path.GetTempFileName(); using var stream = File.OpenWrite(tempfile); await file.CopyToAsync(stream); } + return Results.Accepted(); }); app.Run(); \ No newline at end of file diff --git a/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/index.html b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/index.html deleted file mode 100644 index 4748bf1e7d1c..000000000000 --- a/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/index.html +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - JS Bin - - -
- Swac - -
- - \ No newline at end of file From dee159e66ef0ad558a0adedab94c6714e512792f Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Sat, 16 Jul 2022 16:14:35 +0100 Subject: [PATCH 7/8] added comments --- .../fundamentals/minimal-apis/samples/IFormFile/Program.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs index 5bce0d8c4cb2..a61d641ca0ed 100644 --- a/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs @@ -2,7 +2,10 @@ var app = builder.Build(); +// End point to support a single file upload app.MapGet("/singleFile", () => Results.Text(@"
Swac
", "text/html")); + +// End point to support multiple file uploads app.MapGet("/multipleFiles", () => Results.Text(@"
Swac
", "text/html")); app.MapPost("/fileup", async (IFormFile file) => { From b8d51b631f9db5c03fea54aa431502e1755d89e3 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Sat, 16 Jul 2022 16:15:19 +0100 Subject: [PATCH 8/8] added spacing --- .../fundamentals/minimal-apis/samples/IFormFile/Program.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs index a61d641ca0ed..afdc51ce29bd 100644 --- a/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs @@ -7,6 +7,7 @@ // End point to support multiple file uploads app.MapGet("/multipleFiles", () => Results.Text(@"
Swac
", "text/html")); + app.MapPost("/fileup", async (IFormFile file) => { // save a file @@ -28,4 +29,4 @@ return Results.Accepted(); }); -app.Run(); \ No newline at end of file +app.Run();