在.NET中调用ajax之后下载文件

时间:2016-10-17 22:45:53

标签: .net ajax excel

我的目标是在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并指定了文件的路径。但是,我不能为我的生活找出文件所在的路径。

1 个答案:

答案 0 :(得分:0)

如何将数据发布到此操作。

如果是来自客户端浏览器的普通表单,那么文件将被下载到浏览器。

如果是ajax请求,那么当响应被发送到ajax成功回调时,您将无法下载该文件。你需要从你的javascript(框架)模拟普通的浏览器表单发布到ajax调用。

请发布客户端代码以获取更多详细信息。此外,您的服务器上没有文件。它将作为每个请求实例的二进制创建,并将被发送给客户。

相关问题