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..afdc51ce29bd --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/samples/IFormFile/Program.cs @@ -0,0 +1,32 @@ +var builder = WebApplication.CreateBuilder(args); + +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) => +{ + // save a file + string tempfile = Path.GetTempFileName(); + using var stream = File.OpenWrite(tempfile); + await file.CopyToAsync(stream); + return Results.Accepted(); +}); + +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(); 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": "*" +}