diff --git a/fundamentals/minimal-apis/samples/IFormFile/MyUtils.cs b/fundamentals/minimal-apis/samples/IFormFile/MyUtils.cs new file mode 100644 index 0000000..a524b18 --- /dev/null +++ b/fundamentals/minimal-apis/samples/IFormFile/MyUtils.cs @@ -0,0 +1,39 @@ + +public static class MyUtils +{ + static string GetOrCreateFilePath(string fileName, string contentRootPath, + string filesDirectory = "uploadFiles") + { + var directoryPath = Path.Combine(contentRootPath, filesDirectory); + Directory.CreateDirectory(directoryPath); + return Path.Combine(directoryPath, fileName); + } + + public static async Task SaveFileWithName(IFormFile file, string fileSaveName, string contentRootPath) + { + var filePath = GetOrCreateFilePath(fileSaveName, contentRootPath); + await using var fileStream = new FileStream(filePath, FileMode.Create); + await file.CopyToAsync(fileStream); + } + + public static string GenerateHtmlForm(string formFieldName, string requestToken) + { + return $""" + +
+ + + + """; + } +} diff --git a/fundamentals/minimal-apis/samples/IFormFile/Program.cs b/fundamentals/minimal-apis/samples/IFormFile/Program.cs index 9d00025..d74139d 100644 --- a/fundamentals/minimal-apis/samples/IFormFile/Program.cs +++ b/fundamentals/minimal-apis/samples/IFormFile/Program.cs @@ -10,26 +10,24 @@ builder.Services.AddAntiforgery(); var app = builder.Build(); app.UseAntiforgery(); -var MyHtml = new MyUtils(); - // Generate a form with an anti-forgery token and an /upload endpoint. app.MapGet("/", (HttpContext context, IAntiforgery antiforgery) => { var token = antiforgery.GetAndStoreTokens(context); - var html = MyHtml.GenerateHtmlForm(token.FormFieldName, token.RequestToken!); + var html = MyUtils.GenerateHtmlForm(token.FormFieldName, token.RequestToken!); return Results.Content(html, "text/html"); }); app.MapPost("/upload", async Task