Adding JWT to SaveUrl HTTP POST

I need to pass along the JWT with the HTTP POST for saving file chunks.

I've seen references to adding HTTP headers for other controls, but I haven't seen it mentioned on how to do this for the Blazor File Upload control.

Is this possible?

Thanks,
Kirk

10 Replies 1 reply marked as answer

SN Sevvandhi Nagulan Syncfusion Team July 6, 2020 06:05 AM UTC

Hi Kirk,   
  
    
Greetings from Syncfusion support.  
    
We have validated your reported query for “adding HTTP headers”. The uploader component allows you to add additional header on file upload and can be received in the server-side. You can achieve this behaviour using FileSelected / BeforeUpload event and its CurrentRequest argument by configure header as key-value pair.    
   
    
[index.razor]   
@using Syncfusion.Blazor.Inputs   
<SfUploader ID="UploadFiles">   
    <UploaderEvents FileSelected="onFileSelect"></UploaderEvents>   
    <UploaderAsyncSettings SaveUrl="api/SampleData/Save">   
    </UploaderAsyncSettings>   
</SfUploader>   
   
@code {   
   
    private void onFileSelect(SelectedEventArgs args)   
    {   
        var accessToken = "Basic test123";   
        args.CurrentRequest = new List<object> { new { Authorization = accessToken } };   
    }   
   
}   
[SampleDataController.cs]   
[HttpPost("[action]")]   
        public async void Save(IList<IFormFile> UploadFiles)   
        {   
            //to get authorization Header to handle save file on server side     
            var authorizationHeader = Request.Headers["Authorization"];   
            try   
            {   
                foreach (var file in UploadFiles)   
                {   
                    if (UploadFiles != null)   
                    {   
                        var filename = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');   
                        filename = hostingEnv.WebRootPath + $@"\{filename}";   
                        if (!System.IO.File.Exists(filename))   
                        {   
                            using (FileStream fs = System.IO.File.Create(filename))   
                            {   
                                file.CopyTo(fs);   
                                fs.Flush();   
                            }   
                        }   
                        else   
                        {   
                            Response.Clear();   
                            Response.StatusCode = 204;   
                            Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File already exists.";   
                        }   
                    }   
                }   
                Response.Headers.Add("ID""Failure"); // Assign the custom data in the response header.   
            }   
            catch (Exception e)   
            {   
                Response.Clear();   
                Response.ContentType = "application/json; charset=utf-8";   
                Response.StatusCode = 204;   
                Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File failed to upload";   
                Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = e.Message;   
            }   
               
        }   

    
  
Screenshot:   
  
  
   
  
  
Please find the sample from the below link.   
    
    
Regards,   
Sevvandhi N  



Marked as answer

KI Kirk July 6, 2020 05:28 PM UTC

Perfect, thanks.  That's just what I needed.


SN Sevvandhi Nagulan Syncfusion Team July 7, 2020 04:54 AM UTC

Hi Kirk, 
 
 
Thanks for the update. Please let us know if you need any other further assistance. 
 
 
Regards, 
Sevvandhi N 



KI Kirk July 8, 2020 04:58 PM UTC

Hi Sevvandhi,

In SfUploader, is there a way to configure the UploaderAsyncSettings (or any other component property) programmatically, from a code-behind?

I'd like to customize the route, and I guessed it may be something like this, but get the error:
Component parameter 'SaveUrl' should not be set outside of its component

        private void OnBeforeUpload(BeforeUploadEventArgs args)
        {
            SfUploader.AsyncSettings.SaveUrl = "...";
            SfUploader.AsyncSettings.RemoveUrl = "...";
        } 

Thanks,
Kirk


SN Sevvandhi Nagulan Syncfusion Team July 9, 2020 03:08 PM UTC


Hi Kirk, 


You can dynamically change the save and remove url in the FileSelected event. Refer to the below code sample. 


 
<SfUploader ID="UploadFiles" AutoUpload="true"> 
    <UploaderEvents FileSelected="onFileSelected"></UploaderEvents> 
    <UploaderAsyncSettings SaveUrl="@saveUrl" RemoveUrl="@removeurl"> 
    </UploaderAsyncSettings> 
</SfUploader> 
 
@code { 
 
    public string saveUrl { get; set; }  
 
    public string removeurl { get; set; }  
 
    protected override void OnInitialized() 
 
    { 
        this.saveUrl = "https://aspnetmvc.syncfusion.com/services/api/uploadbox/Save"; 
        this.removeurl = "https://aspnetmvc.syncfusion.com/services/api/uploadbox/Remove"; 
    } 
 
    public void onFileSelected(SelectedEventArgs e) 
    { 
        this.saveUrl = "https://localhost:44394/api/SampleData/Save"; 
        this.removeurl = "https://localhost:44394/api/SampleData/Remove"; 
    } 
 
} 




Regards, 
Sevvandhi N 



KI Kirk July 9, 2020 06:01 PM UTC

Perfect, thanks!


SN Sevvandhi Nagulan Syncfusion Team July 10, 2020 04:52 AM UTC

Hi Kirk, 


Thanks for the update. Please let us know if you need any other further assistance. 

Regards, 
Sevvandhi N 



MI Mike-E April 2, 2022 07:03 PM UTC

I hate to resurrect an old thread, but it's for a good reason: to thank you for having this feature!  Really nice work out there, Team Syncfusion. 👍



MI Mike-E April 3, 2022 04:44 PM UTC

Doh, actually, I ran into a problem.  I've posted a new thread here if interested:

https://www.syncfusion.com/forums/174108/refused-to-set-unsafe-header-cookie




RP Ranjani Prabakaran Syncfusion Team April 4, 2022 12:53 PM UTC

Hi Mike,


You are most welcome. Please follow-up with the newly created thread for further assistance.


Regards,


Ranjani

Loader.
Up arrow icon