see min api parameter binding for binding to file uploads:
the browser only supports multiple file selects for upload, not folders.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I want to upload files and folders recursively throught the wasm client to minimal api server.
I have learned that I can use IFormFile or IFormFileCollection to receive files in minimal api.
see min api parameter binding for binding to file uploads:
the browser only supports multiple file selects for upload, not folders.
Below is a sample of Blazor Web Assembly (WASM) app which can upload file by using jQuery ajax (I don't know what you mean by "recursively" though):
Razor page for file upload
@page "/fileupload"
@inject IJSRuntime JSRuntime;
<h3>File Upload by jQuery Ajax</h3>
<form id="form1" method="post" enctype="multipart/form-data">
<input type="file" name="postedfile" />
</form>
<button type="button" class="btn btn-primary" @onclick="UploadFile">
Upload
</button>
<div id="result"></div>
@code {
private async Task UploadFile()
{
await JSRuntime.InvokeVoidAsync("uploadFile");
}
}
exampleJsInterop.js
window.uploadFile = () => {
var fd = new FormData(document.getElementById("form1"));
$.ajax({
url: '/Upload',
method: 'post',
data: fd,
processData: false,
contentType: false
}).done(function (response) {
$("#result").empty;
$("#result").text(response);
}).fail(function (jqXHR, textStatus, errorThrown) {
$("#result").empty;
$("#result").text('textStatus: ' + textStatus +
', errorThrown: ' + errorThrown);
});
}
Add jQuery.js and exampleJsInterop.js
Add script tag to index.html
Add controller to Sever project to recive uploaded file
using Microsoft.AspNetCore.Mvc;
namespace BlazorWasmCoreHosted.Server.Controllers
{
[Route("[controller]")]
[ApiController]
public class UploadController : ControllerBase
{
[HttpPost]
public string Post([FromForm] IFormFile? postedFile)
{
string result = "";
if (postedFile != null && postedFile.Length > 0)
{
string filename = System.IO.Path.GetFileName(postedFile.FileName);
// process of uploaded file - Omitted
result = $"{filename} ({postedFile.ContentType}) - " +
$"{postedFile.Length} bytes Upload Complete";
}
else
{
result = "Upload fail";
}
return result;
}
}
}