I am trying to upload a file from a dialog. (code below) When I try to upload the file, the following error is returned via the error handler:
{"type":"RFC 9110 - HTTP Semantics or more validation errors occurred.","status":400,"errors":{"file":["The file field is required."]},"traceId":"00-ca846be8f542b9fc144fe1d72367310f-47e909119f95a46a-00"}
This refers to the file argument in the signature of my controller method:
[HttpPost("UploadDocument/{docType}/{category:int}/{id}")]
public async Task<ActionResult> UploadDocument(IFormFile file, string docType, int category, string id, string batch = "", string filename = "")
Developer tools shows that no file is included with the request that fails.
What do I need to do to get the file to be sent with the form submission?
Below are the relevant parts of the client code.
<RadzenTemplateForm TItem="string" Submit="@(args=>StartUpload(args))" Data="currentFileName">
<RadzenPanel Variant="Variant.Outlined">
<h3>@Title</h3>
<RadzenPanel>
<RadzenRow Gap="0rem" RowGap="0rem" class="rz-m-0 rz-m-md-12">
<RadzenColumn Size="9">
<RadzenUpload id="ddUpload" @ref="uploadDD"
ChooseText="Drag and drop here or click to choose document"
Auto="false" Multiple="false" Url=@($"ImagingRepository/UploadDocument/{DocumentType}/{(int)DocumentCategory}?filename={currentFileName};documentId={DocumentId}")
InputAttributes="@(new Dictionary<string,object>(){ { "aria-label", "select file" }})"
Change=@UploadFileChanged
Error=@UploadErrorHandler
Complete=@UploadComplete
Progress=@(args => OnProgress(args, "Drag and drop file to upload")) Style="width: 100%" />
</RadzenColumn>
</RadzenRow>
<RadzenRow Gap="0rem" RowGap="0rem" class="rz-m-0 rz-m-md-12">
<RadzenFormField Text="Name" AllowFloatingLabel="true" Variant="Variant.Text" Style="width:100%">
<ChildContent>
<RadzenTextBox Name="tbFileName" @ref="tbFilename" @bind-Value=@currentFileName Style="width:100%" />
</ChildContent>
<Helper>
<RadzenRequiredValidator Component="tbFileName" Text="Name is required." />
</Helper>
</RadzenFormField>
</RadzenRow>
<RadzenRow>
<RadzenColumn Size="12">
<RadzenButton Text="Upload"
ButtonType="ButtonType.Submit" class="rz-mt-4" />
</RadzenColumn>
</RadzenRow>
</RadzenPanel>
</RadzenPanel>
</RadzenTemplateForm>
@code {
RadzenUpload uploadDD;
RadzenTextBox tbFilename;
string currentFileName{get;set;}
FileInfo? currentFile;
private async Task StartUpload(string args)
{
if (null == currentFile) return;
await uploadDD.Upload();
}
private void UploadComplete(UploadCompleteEventArgs args)
{
if (!args.Cancelled && Guid.TryParse(args.RawResponse, out var guid))
{
DocumentId = guid;
DialogService.Close(guid);
}
}
private void UploadErrorHandler(UploadErrorEventArgs args)
{
uploadError = "There was an error uploading the file.";
if (!string.IsNullOrWhiteSpace(args.Message))
uploadError += " " + args.Message;
}
}