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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

UploaderEvents on succes - cannot catch api response

Thread ID:

Created:

Updated:

Platform:

Replies:

151324 Feb 6,2020 10:00 PM UTC Mar 12,2020 03:14 PM UTC Blazor 5
loading
Tags: File Upload
David Dumitru
Asked On February 6, 2020 10:00 PM UTC

I'm trying to get back the response of the api that's called from SaveUrl uploader settings. All I can get is a custom SuccessEventArgs object. My api response sends back an Id (int) which I need for other data manipulation. Is there any way to customize the SuccesEventArgs object from my response or anything else I could do to achieve what I want?

Thank you,
David.

Berly Christopher [Syncfusion]
Replied On February 7, 2020 02:09 PM UTC

Hi David, 
 
Greetings from Syncfusion support.  
 
We can pass the custom data in the response header and get the passed data in the success event arguments form args.Response.Headers as in the below code example. 
 
[index.razor] 
 
<EjsUploader ID="UploadFiles" AutoUpload="false"> 
<UploaderEvents Success="OnSuccess"></UploaderEvents> 
<UploaderAsyncSettings SaveUrl="api/SampleData/Save" RemoveUrl="api/SampleData/Remove"></UploaderAsyncSettings> 
</EjsUploader> 
@code{ 
  
public void OnSuccess(SuccessEventArgs args) 
{ 
var customHeader = args.Response.Headers.Split(new Char[] { '\n' })[1]; // To split the response header values 
var key = customHeader.Split(new Char[] { ':' })[0]; // To get the key pair of provided custom data in header 
var value = customHeader.Split(new Char[] { ':' })[1].Trim(); // To get the value for the key pair of provided custom data in header 
} 
} 
[SampleDataController.cs] 
 
[HttpPost("[action]")] 
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", "custom_ID"); // 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 = "No Content"; 
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = e.Message; 
} 
  
} 
 
Please find the sample from the below link. 
 
 
Regards, 
Berly B.C 


David Dumitru
Replied On February 7, 2020 02:13 PM UTC

Thank you very much, that solved my problem. 

Cheers!

Sureshkumar P [Syncfusion]
Replied On February 10, 2020 07:17 AM UTC

Hi David, 
 
 
Regards, 
Sureshkumar P 


j
Replied On March 7, 2020 05:37 AM UTC

Debugging the args from OnSuccess() method, there's StatusText. I'm imagining I can populate it from the API response. Is there a way I can do that?

Basically the API responses with a lot of string. So I want to put it in the response body not in the header as what the sample has given.



Attachment: EjsUploader_9515f5ea.zip

Berly Christopher [Syncfusion]
Replied On March 12, 2020 03:14 PM UTC

Hi J, 
  
Thanks for the update. 
  
Query 1: 
  
Debugging the args from OnSuccess() method, there's StatusText. I'm imagining I can populate it from the API response. Is there a way I can do that?  So I want to put it in the response body not in the header as what the sample has given. 
  
Response: 
  
No, while given the status message in the server-side, success event arguments won’t receive it in the StatusText. So, we can achieve this by sending the status message on the server-side as additional data in the header and receiving it in the success event arguments response header as provided in the previous update. 
  
Query 2: 
  
Basically the API responses with a lot of string.  
  
Response: 
  
Indeed, a lot of strings come up with the comment header. So only by using JSON stringify as stated in the previous update we have split the status text and got the additionally sent data in the success event. 
  
Regards, 
Berly B.C 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon