diff --git a/BlazorAppTest/Controllers/FileUploadController.cs b/BlazorAppTest/Controllers/FileUploadController.cs index 872e225c..6d5300b3 100644 --- a/BlazorAppTest/Controllers/FileUploadController.cs +++ b/BlazorAppTest/Controllers/FileUploadController.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Mvc; +using BlazorAppTest.Infrastructure; +using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.WebUtilities; using Microsoft.Net.Http.Headers; using System.Net; @@ -12,7 +13,8 @@ public class FileUploadController : ControllerBase // https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads#upload-large-files-with-streaming // https://github.com/dotnet/AspNetCore.Docs/tree/master/aspnetcore/mvc/models/file-uploads/samples/ [HttpPost("/file-upload-streamed/")] - public async Task UploadStreamedFile() + [DisableFormValueModelBinding] + public async Task UploadStreamedFile(CancellationToken cancellationToken) { if (!IsMultipartContentType(Request.ContentType)) { @@ -22,7 +24,7 @@ public async Task UploadStreamedFile() var boundary = GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType), lengthLimit: BoundaryLengthLimit); var reader = new MultipartReader(boundary, HttpContext.Request.Body); - var section = await reader.ReadNextSectionAsync(); + var section = await reader.ReadNextSectionAsync(cancellationToken); while (section != null) { @@ -33,16 +35,17 @@ public async Task UploadStreamedFile() // Don't trust the file name sent by the client. To display the file name, HTML-encode the value. var trustedFileNameForDisplay = WebUtility.HtmlEncode(contentDisposition.FileName.Value); var trustedFileNameForFileStorage = Path.GetRandomFileName(); - using (var targetStream = System.IO.File.Create(Path.Combine(Path.GetTempPath(), trustedFileNameForDisplay /* trustedFileNameForFileStorage */))) // TOTO JE JENOM TESTOVÁTKO, NIKDY SOUBORY POD PŮVODNÍM NÁZVEM + // THIS IS JUST FOR TESTING, NEVER SAVE FILES UNDER THE ORIGINAL NAME + using (var targetStream = System.IO.File.Create(Path.Combine(Path.GetTempPath(), trustedFileNameForDisplay /* trustedFileNameForFileStorage */))) { - await section.Body.CopyToAsync(targetStream); + await section.Body.CopyToAsync(targetStream, cancellationToken); } return Ok(trustedFileNameForFileStorage); } // Drain any remaining section body that hasn't been consumed and read the headers for the next section. - section = await reader.ReadNextSectionAsync(); + section = await reader.ReadNextSectionAsync(cancellationToken); } return BadRequest(); diff --git a/BlazorAppTest/Infrastructure/DisableFormValueModelBindingAttribute.cs b/BlazorAppTest/Infrastructure/DisableFormValueModelBindingAttribute.cs new file mode 100644 index 00000000..86c5f67b --- /dev/null +++ b/BlazorAppTest/Infrastructure/DisableFormValueModelBindingAttribute.cs @@ -0,0 +1,20 @@ +using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.AspNetCore.Mvc.ModelBinding; + +namespace BlazorAppTest.Infrastructure; + +[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] +public class DisableFormValueModelBindingAttribute : Attribute, IResourceFilter +{ + public void OnResourceExecuting(ResourceExecutingContext context) + { + var factories = context.ValueProviderFactories; + factories.RemoveType(); + factories.RemoveType(); + factories.RemoveType(); + } + + public void OnResourceExecuted(ResourceExecutedContext context) + { + } +} \ No newline at end of file diff --git a/Havit.Blazor.Documentation.Server/Controllers/FileUploadControllerDemo.cs b/Havit.Blazor.Documentation.Server/Controllers/FileUploadControllerDemo.cs index 794fa4a0..49139b52 100644 --- a/Havit.Blazor.Documentation.Server/Controllers/FileUploadControllerDemo.cs +++ b/Havit.Blazor.Documentation.Server/Controllers/FileUploadControllerDemo.cs @@ -1,4 +1,5 @@ using System.Net; +using Havit.Blazor.Documentation.Server.Infrastructure; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.WebUtilities; using Microsoft.Net.Http.Headers; @@ -12,7 +13,8 @@ public class FileUploadControllerDemo : ControllerBase // https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads#upload-large-files-with-streaming // https://github.com/dotnet/AspNetCore.Docs/tree/master/aspnetcore/mvc/models/file-uploads/samples/ [HttpPost("/file-upload-streamed/")] - public async Task UploadStreamedFile() + [DisableFormValueModelBinding] + public async Task UploadStreamedFile(CancellationToken cancellationToken) { if (!IsMultipartContentType(Request.ContentType)) { @@ -22,7 +24,7 @@ public async Task UploadStreamedFile() var boundary = GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType), lengthLimit: BoundaryLengthLimit); var reader = new MultipartReader(boundary, HttpContext.Request.Body); - var section = await reader.ReadNextSectionAsync(); + var section = await reader.ReadNextSectionAsync(cancellationToken); while (section != null) { @@ -37,16 +39,16 @@ public async Task UploadStreamedFile() var trustedFileNameForFileStorage = Path.GetRandomFileName(); // using (var targetStream = System.IO.File.Create(Path.Combine(Path.GetTempPath(), trustedFileNameForFileStorage))) // { - // await section.Body.CopyToAsync(targetStream); + // await section.Body.CopyToAsync(targetStream, cancellationToken); // } - await section.Body.CopyToAsync(Stream.Null); + await section.Body.CopyToAsync(Stream.Null, cancellationToken); return Ok(trustedFileNameForFileStorage); } // Drain any remaining section body that hasn't been consumed and read the headers for the next section. - section = await reader.ReadNextSectionAsync(); + section = await reader.ReadNextSectionAsync(cancellationToken); } return BadRequest(); diff --git a/Havit.Blazor.Documentation.Server/Infrastructure/DisableFormValueModelBindingAttribute.cs b/Havit.Blazor.Documentation.Server/Infrastructure/DisableFormValueModelBindingAttribute.cs new file mode 100644 index 00000000..142c3ad2 --- /dev/null +++ b/Havit.Blazor.Documentation.Server/Infrastructure/DisableFormValueModelBindingAttribute.cs @@ -0,0 +1,20 @@ +using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.AspNetCore.Mvc.ModelBinding; + +namespace Havit.Blazor.Documentation.Server.Infrastructure; + +[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] +public class DisableFormValueModelBindingAttribute : Attribute, IResourceFilter +{ + public void OnResourceExecuting(ResourceExecutingContext context) + { + var factories = context.ValueProviderFactories; + factories.RemoveType(); + factories.RemoveType(); + factories.RemoveType(); + } + + public void OnResourceExecuted(ResourceExecutedContext context) + { + } +}