I added the following inject to my page
@inject IAccessTokenProvider TokenProvider
Then I added the following to the OnUpload handler for including the bearer token:
AccessTokenResult tokenResult = await TokenProvider.RequestAccessToken();
if (tokenResult.TryGetToken(out AccessToken token))
{
//Add the bearer token to the authorization header.
args.RequestHeaders.Add("Authorization", $"Bearer {token.Value}");
}
An idea for a workaround is to, perhaps, hide the Retry button altogether for the users, with a bit of CSS:
<style>
.k-upload-status .k-upload-action:nth-of-type(2n+1) {
display: none;
}
</style>
@inject NavigationManager NavigationManager
<TelerikUpload SaveUrl="@SaveUrl"
RemoveUrl="@RemoveUrl"
OnUpload="@OnUploadHandler">
</TelerikUpload>
@code {
async Task OnUploadHandler(UploadEventArgs e)
{
// a flag to make uploads error out, see the controller
e.RequestData.Add("shouldFail", "true");
}
// a sample way of generating the URLs to the endpoint
public string SaveUrl => ToAbsoluteUrl("api/upload/save");
public string RemoveUrl => ToAbsoluteUrl("api/upload/remove");
public string ToAbsoluteUrl(string url)
{
return $"{NavigationManager.BaseUri}{url}";
}
}
and the controller just throws for that test case:
[HttpPost]
public async Task<IActionResult> Save(IEnumerable<IFormFile> files)
{
string shouldFail = Request.Form["shouldFail"]; // the key from the OnUpload event that will make the request fail
if (shouldFail != null && shouldFail.ToLowerInvariant().Equals("true"))
{
Response.StatusCode = 401;
return new EmptyResult();
}
Regards,
Marin Bratanov
Progress Telerik
Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive, special prizes, and more, for FREE?! Register now for DevReach 2.0(20).