Completed
Last Updated: 09 Dec 2021 18:06 by Samuel
Release 2.28.0
SL
Created on: 13 May 2020 16:40
Category: Upload
Type: Bug Report
12
The Retry button does not fire OnUpload and you can't attach auth tokens
If a request fails once, the upload will give you a Retry button. This button does not fire OnUpload again, so the request no longer has the custom logic (such as bearer tokens).
2 comments
Samuel
Posted on: 09 Dec 2021 18:06

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}");
            }

 

ADMIN
Marin Bratanov
Posted on: 13 Oct 2020 07:30

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).