Download byte array data using jQuery AJAX in ASP.Net Core MVC

pandeygolu4200
 
on Sep 27, 2022 03:29 AM
352 Views

How to download byte array data from jQuery Ajax.

I have one api which return me data as Byte[], i want to download that data in ajax jquery response.

How can i download that file.

public IActionResult DownloadProviderDocument(DownloadProviderDocumentModel model)
{
    AccountApiCall accountApiCall = new AccountApiCall(Configuration["PrescriptionAPI"]);            
    var Response = accountApiCall.PostAPIcall<ProviderDownloadDocumentResponseModel>(model, "provider/documentdownload", HttpContext.Request.Cookies["Token"].ToString());           
    return Json(Response);
}
$('#dataexample tbody').on('click', 'a.btndownloadlink', function () {                              
    var $row = $(this).closest('tr');
    var data =  $('#dataexample').DataTable().row($row).data();                 
     $.ajax({
         type: "POST",
         url: '@Url.Action("DownloadProviderDocument","Provider")',
         data: {UserId: data.userId,File:data.fileName },                         
         success: function (response) {
             alert('image');
              $.toast({
                 heading: 'Success',
                 text: 'Document Downloaded  Successfully!.',
                 showHideTransition: 'slide',
                 icon: 'success'
             }); 
            // window.location.reload();
             var table=('#dataexample').Datatable();
             table.draw();
             $("#dataexample").ajax.reload();
             $("#providertab1").removeClass("active");
             $("#step1").removeClass("active");
 
             //add second tab active class
             $("#providertab2").addClass("active");
             $("#step2").addClass("active");
         }
    });              
});

the json return data like below.

/9j/4AAQSkZJRgABAQEASABIAAD.....

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 27, 2022 03:37 AM