[英]Download file after ajax post call in .NET
我的目標是在ajax POST調用后下載Excel文件。 到目前為止,我已經有了這個端點,可以生成我想要的excel文件:
[HttpPost]
public ActionResult ExportReportList(ReportFilterViewModel filter, RequestedPageInfo pageInfo)
{
IEnumerable<RtbReport> rtbs = null;
// get collection of page view entities to supply to the exporter
rtbs = RtbReportRepository.GetReport(filter).ToList();
// put in default values for null-valued numberic fields
rtbs = rtbs.ToList()
.Select(s =>
{
s.AdvertiserId = s.AdvertiserId ?? 0;
s.CampaignId = s.CampaignId ?? 0;
s.RtbCampaignId = s.RtbCampaignId ?? 0;
s.CapLoss = s.CapLoss ?? 0.0M;
s.GrossRevenue = s.GrossRevenue ?? 0.0M;
s.GrossProfit = s.GrossProfit ?? 0.0M;
s.GrossProfitPerc = s.GrossProfitPerc ?? 0.0M;
return s;
});
// export the report as an XLS binary format
byte[] output = GetReportListExcel(rtbs);
// export as an excel MIME type
return File(output, "application/vnd.ms-excel", "RtbReport.xls");
}
我想下載我生成的這個文件。 我應該點擊哪個路徑來提示下載RtbReport.xls? 從我查找的一些答案中,許多答案都使用window.location並指定了文件的路徑。 但是,我一輩子都無法弄清楚文件所在的路徑。
您如何將數據發布到此操作。
如果是從客戶端瀏覽器發布的普通表格,則文件將下載到瀏覽器。
如果是ajax請求,則由於響應發送到ajax成功回調,因此您將無法下載文件。 您需要從javascript(框架)模擬正常的瀏覽器表單發布,然后才能進行ajax調用。
請發布客戶代碼以獲取更多詳細信息。 此外,您的服務器上沒有文件。 將僅按請求實例的二進制數創建它,並將其發送給客戶。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.