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

UploaderEvents on succes - cannot catch api response

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.

5 Replies

BC Berly Christopher Syncfusion Team 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[] { '' })[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 



DD David Dumitru February 7, 2020 02:13 PM UTC

Thank you very much, that solved my problem. 

Cheers!


SP Sureshkumar P Syncfusion Team February 10, 2020 07:17 AM UTC

Hi David, 
 
 
Regards, 
Sureshkumar P 



J j 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


BC Berly Christopher Syncfusion Team 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 


Loader.
Live Chat Icon For mobile
Up arrow icon