We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Unable to get URL after upload

Hello,

I've made several projects with your file upload to upload files to azure blob storage, get a url, then return it in the response headers.

I'm starting to work on a new project and have tried to achieve the same thing but have not been able to using version 18.4.0.42.
The code I've used is exactly the same as the code that was working in 18.3.0.51.

Im using the OnSuccess Event args to pull out the header I've added to the response, but it only ever has the first line of the headers.

See screenshots below for what im returning in the headers and what is coming through in the SuccessEventArgs.



    public void OnSuccess(SuccessEventArgs args)
    {
        var customHeader = args.Response.Headers.Split(new Char[] { '' })[2]; // To split the response header values
        url = customHeader.Split(new Char[] { ':' }, 2)[1].Trim(); // To get the value for the key pair of provided custom data in header
    }






9 Replies 1 reply marked as answer

PM Ponmani Murugaiyan Syncfusion Team March 8, 2021 11:38 AM UTC

Hi Rhys, 

Greetings from Syncfusion support. 

We checked your reported query, we can get the response headers with collection of strings in the success event arguments. So, if you need to get the data, then we need to split the header content and proceed further based on the application needs as like below highlighted code. 


[Index.razor] 
 
<SfUploader ID="UploadFiles" AutoUpload="false"> 
    <UploaderEvents Success="OnSuccess"></UploaderEvents> 
    <UploaderAsyncSettings SaveUrl="api/SampleData/Save" RemoveUrl="api/SampleData/Remove"></UploaderAsyncSettings> 
</SfUploader> 
 
<p>key value is: @key</p> 
<p>pair value is: @value</p> 
 
    @code{ 
        public string customHeader { get; set; } = ""; 
        public string key { get; set; } = ""; 
        public string value { get; set; } = ""; 
        public void OnSuccess(SuccessEventArgs args) 
        { 
            var customHeader = new string[] { }; 
            customHeader = args.Response.Headers.Split(new Char[] { '' }); // To split the response header values 
            for (var i = 0; i < customHeader.Length; i++) 
            { 
                if (customHeader[i].Split(new Char[] { ':' })[0] == "id") 
                { 
                    key = customHeader[i].Split(new Char[] { ':' })[0]; // To get the key pair of provided custom data in header 
                    value = customHeader[i].Split(new Char[] { ':' })[1].Trim(); // To get the value for the key pair of provided custom data in header 
                } 
            } 
        } 
    } 

[HomeController.cs] 
 
public async void Save(IList<IFormFile> UploadFiles) 
        { 
            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", "Updated"); 
                } 
            } 

Output: 

 

 
Please get back us if you need further assistance on the above provided sample. 

Regards, 
Ponmani M 



RG Rhys Gordon March 9, 2021 01:27 AM UTC

Hi Ponmani,

Thanks for the fast response. 
I must be doing something wrong then, because as per my screenshots only the first header is being passed in, when there are 6 headers.

I'll start again and see if I can get it working. I'll let you know how I go.



PM Ponmani Murugaiyan Syncfusion Team March 9, 2021 06:11 AM UTC

Hi Rhys, 

Welcome. If you need further assistance, please revert us with your query. We always happy to assist you. 

Regards, 
Ponmani M 



RG Rhys Gordon March 9, 2021 09:29 AM UTC

Hi Ponmani,

Ive tried again using a fresh project and your sample as a base but I have been unsucessful.

I should have mentioned earlier, I'm not using the ASP.NET Hosted blazor WASM. Just a blazor WASM with a separate ASP.NET 5 API.
Ive attached the solution from my test in the hopes you can replicate my results.

I copy and pasted most of your code, but moved it into the two separate projects and added a default CORs policy so they can connect, other than that everything is from the standard templates.

Looking in chrome dev tools I can see the header im after is there, but the args.Response.Headers does not contain the header I am after.

Hopefully there is something simple I am not understanding but I just cannot get this to work.

Attachment: UploadTest_4a7b4eeb.rar


RG Rhys Gordon March 11, 2021 10:44 PM UTC

Hello,

Just wondering if anyone has been able to look at this yet. 
I'm hoping to present something to my client soon and this file uploader is the last component I'm waiting on.

Regards,
Rhys


PM Ponmani Murugaiyan Syncfusion Team March 15, 2021 12:47 PM UTC

Hi Rhys, 

Sorry for the delay. 

Currently we are working on the reported query. We will check and update further details in 2 business days. 

Regards, 
Ponmani M 



RG Rhys Gordon March 21, 2021 03:06 AM UTC

Hello,

Just wondering if there is any update on this.


PM Ponmani Murugaiyan Syncfusion Team March 22, 2021 12:45 PM UTC

Hi Rhys, 
 
We deeply regret for the inconvenience caused. 
 
We able to get the response headers in server side, but we were facing issue in the client side Blazor application. So we investigating further to find the solution for the reported issue in WASM. We need two more business days to validate further in our end. We appreciate your patience until then. 
 
Regards, 
Ponmani M 



PM Ponmani Murugaiyan Syncfusion Team March 26, 2021 12:56 PM UTC

Hi Rhys, 

Thanks for your valuable patience. 

Based on further investigation on the above mentioned issue, we would like to know you that in the blazor WASM application, there is no need to use cookies for authentication. So browser cross origins, headers are removed, and the response headers are not returned in the success event. As a result, we recommend calling the AllowCredentials() method, as shown in the code snippet below, to allow header configuration in the startup page. 

[WebAPI/Startup.cs ] 

services.AddCors(options => 
            { 
                options.AddDefaultPolicy( 
                    builder => 
                    {    
                    builder.WithOrigins("https://localhost:5001") 
                           .AllowAnyMethod() 
                           .AllowAnyHeader() 
                           .AllowCredentials() 
                           .WithExposedHeaders("*"); 
            }); 
}); 
 


Regards, 
Ponmani M 


Marked as answer
Loader.
Live Chat Icon For mobile
Up arrow icon