Hello.
I'm using the Upload component in a project and I'm mostly simply coping the code from the examples.
this is my controller code:
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
namespace CRM.Controller
{
[DisableRequestSizeLimit]
public partial class UploadController : ControllerBase
{
private readonly IWebHostEnvironment _webHostEnvironment;
private string uploadDirectory { get; set; } = "uploads";
public UploadController(IWebHostEnvironment webHostEnvironment)
{
_webHostEnvironment = webHostEnvironment;
}
[HttpPost("upload/single")]
public async Task<IActionResult> SingleUpload(IFormFile file)
{
try
{
await UploadFile(file, "template");
return Ok(file.FileName);
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
throw;
}
}
[HttpPost("upload/multiple")]
public async Task<IActionResult> MultipleUpload(IFormFile[] files)
{
try
{
foreach(var item in files)
{
await UploadFile(item, "template");
}
return StatusCode(200);
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
throw;
}
}
public async Task UploadFile(IFormFile file, string folder)
{
if ((file != null) && (file.Length > 0))
{
var uploadPath = Path.Combine(_webHostEnvironment.WebRootPath, uploadDirectory, folder) ;
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
}
var fullPath = Path.Combine(uploadPath, file.FileName);
await using (FileStream fs = new FileStream(fullPath, FileMode.Create, FileAccess.Write))
{
await file.CopyToAsync(fs);
}
}
}
}
}
in my razor component page I have the following snippet of code:
<div class="row m-0">
<RadzenText TextStyle="TextStyle.Subtitle1" TagName="TagName.H3">
Nome Template
</RadzenText>
</div>
<div class="row p-2">
<div class="col-md-10">
<RadzenTextBox @ref="templateName" Change=@(args => OnChange(args, "TemplateName")) Placeholder="Nome Template..." class="w-100" />
</div>
<div class="col-md-2">
<RadzenUpload Url="upload/single" Accept="text/html" Progress="@((args) => OnProgress(args, "Single file upload"))" Complete=@CompleteUpload
ChooseText="Selezione Template..." class="float-end"></RadzenUpload>
</div>
</div>
The upload functionality works as a charm, the only problem is that when I upload a file the page is reloaded and all the data in the other data gets reset (dropdowns as default, textboxes empty and so on).
Note: I'm uploading an HTML files, maybe this is the issue?
Any help on what I'm doing wrong?