如何使用单个ajax调用下载文件?

时间:2018-12-04 11:32:31

标签: javascript c# asp.net-web-api

这是我的JavaScript代码:

$.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.");
  }
})

但是在这里我两次调用了api。首先,我检查是否有可用数据,然后我将要求下载如何通过单次调用来实现。

0 个答案:

没有答案