0

This is my JavaScript code:

$.ajax({
  type: 'GET',
  url: GlobalConstant.serviceURL + "/CampaignReport/GetCampaignWiseTransactionData2?CampaignId=" + $scope.tempFilter.campaignId + "&endDate=" + JSON.parse(JSON.stringify($scope.tempFilter.endDate)) + "&startDate=" + JSON.parse(JSON.stringify($scope.tempFilter.startDate)),
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function (res) {
              if (typeof res === 'string') {
                toastr.error(res); // Data Not Available
              }  
              else {
              //Return ResponseMessageResult of C#
              window.location.href = GlobalConstant.serviceURL + "/CampaignReport/GetCampaignWiseTransactionData2?CampaignId=" + $scope.tempFilter.campaignId + "&endDate=" + JSON.parse(JSON.stringify($scope.tempFilter.endDate)) + "&startDate=" + JSON.parse(JSON.stringify($scope.tempFilter.startDate));
          }
  },
  error: function (res) {
  //Server Error
    toastr.error("Oops something went wrong, Please try again later.");
  }
})

But here I am calling the api two times. first I check there is data available or not if data available then I will call for download how can I achieve in single call.

J.vee
  • 623
  • 1
  • 7
  • 26
Jasmin Solanki
  • 369
  • 7
  • 26
  • What kind of file is being downloaded? Do you want it loaded and viewed inside the browser? Or saved / prompted to save to the hard drive? – mjwills Dec 04 '18 at 11:46
  • i want to fatch data from database and then write .xlsx file and download it – Jasmin Solanki Dec 04 '18 at 11:48
  • Saved in harddrive – Jasmin Solanki Dec 04 '18 at 11:49
  • Why do you want to do it in only a single request? Is that a **definite** requirement? – mjwills Dec 04 '18 at 11:52
  • yes because first i need to fire query in database there is data or not its take 10 minutes after that i will fire request for download its also take 10 minutes so for performance issue i need in single request – Jasmin Solanki Dec 04 '18 at 11:54
  • The browser is going to drop the download request well before 10 minutes is up. – mjwills Dec 04 '18 at 11:56
  • Possible duplicate of [Download a file by jQuery.Ajax](https://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax). JS can not download files due to security reasons, but there is a workaround using [jQuery.FileDownload](http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/). – Georg Patscheider Dec 04 '18 at 12:42

0 Answers0