Return back information on upload

Hello
I have an upload control that stores the file on a SQL database. 
After the save operation is done I'd like to return back the ID of the file on the database but I don't know where to catch this information on the View. I tried the success event but no Id is on the args parameters.
I replaced the code on the controller and I finish with "return Json(fileId);".
Any idea?

Thanks in advance

3 Replies

SI Silambarasan I Syncfusion Team October 2, 2018 09:50 AM UTC

Hi Toni, 
 
Thank you for contacting Syncfusion support. 
 
Before we provide solution, we would like to know more about your requirement i.e., you have mentioned ‘I'd like to return back the ID of the file’. Please confirm whether you want to return custom parameter value like (ID) or to need any default file properties value in ‘success’ event. 
 
Meanwhile, we would like to let you know that the success event argument returns only the file information. However, we have provided an option to return HttpResponse from server that can be obtained at success event argument. Please refer the below code example. 
 
Script section: 
let uploadObj: Uploader = new Uploader({ 
    success: OnSuccess 
}); 
uploadObj.appendTo('#fileupload') 
function OnSuccess(args: any): void { 
     console.log(args.e.target.statusText); 
} 
 
C#: 
[AcceptVerbs("Post")] 
public void Save() 
{ 
HttpResponse Response = System.Web.HttpContext.Current.Response; 
Response.Clear(); 
Response.Status = "204 File already exists"; 
Response.StatusText = "File already exists"; 
Response.End(); 
} 
 
Could you please check the above information and let us know whether is this fulfilling your requirement, if not please share us more information regarding this so that we can analyze based on that and provide you a better solution. 
 
Regards, 
Silambarasan 



RA Ryan Allan July 28, 2021 03:17 PM UTC

I too am wondering how to accomplish this, and in my case, it is a custom parameter like ID that I need returned to the view. How do I accomplish this?



BC Berly Christopher Syncfusion Team July 30, 2021 02:14 PM UTC

Hi Toni, 
  
We can send the additional data such as “id or anything” from the response headers and get it from the response header of the success event arguments as mentioned below. 
  
@{ 
    var asyncSettings = new Syncfusion.EJ2.Inputs.UploaderAsyncSettings { SaveUrl = "api/Values/Save", RemoveUrl = "api/Values/Remove" }; 
} 
<ejs-uploader id="uploadFiles" asyncSettings="@asyncSettings" autoUpload="false" success="OnSucess"></ejs-uploader> 
 
<script> 
    function OnSucess(args) { 
        console.log(JSON.parse(JSON.stringify(args.response.headers).split("\\r\\n")).split(",")[2]); 
    } 
</script> 
[ValuesController.cs] 
    [Obsolete] 
        public void Save(IList<IFormFile> UploadFiles) 
        { 
            try 
            { 
                foreach (var file in UploadFiles) 
                { 
                    var filename = ContentDispositionHeaderValue 
                                        .Parse(file.ContentDisposition) 
                                        .FileName 
                                        .Trim('"'); 
                    filename = hostingEnv.ContentRootPath + $@"\{filename}"; 
                    long size = 0; 
                    size += file.Length; 
                    if (!System.IO.File.Exists(filename)) 
                    { 
                        using (FileStream fs = System.IO.File.Create(filename)) 
                        { 
                            file.CopyTo(fs); 
                            fs.Flush(); 
                        } 
                    } 
                } 
                Response.Headers.Add("ID", "Failure"); // Assign the custom data in the response header.  
            } 
            catch (Exception e) 
            { 
                Response.Clear(); 
                Response.StatusCode = 204; 
                Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File failed to upload"; 
                Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = e.Message; 
            } 
        } 
 
  
  
Regards, 
Berly B.C 


Loader.
Up arrow icon