来自ajax响应的Angular 1下载文件

时间:2018-01-23 05:54:32

标签: javascript angularjs ajax asp.net-mvc

我使用EPPLUS在我的MVC控制器中生成一个excel文件。 我正在使用ajax请求从角度js控制器调用控制器方法

 $scope.agreedFlow = function () {

        var ajaxexcelDownload = AgreedFlowService.GenerateAgreedFlow({
        });

        $q.all([ajaxexcelDownload]).then(function (responses) {

        });
    };

我的角色服务

self.GenerateAgreedFlow = function (data, callback) {
            ajaxService.post({
                url: '/Plan/GenerateAgreedFlow'
                , data: data
                , dataType: 'json'
                , responseType: 'arraybuffer'
                , cache: false
            }).done(function (result) {
                if (typeof callback === "function")
                    callback(result);

            }).fail(function () {
            });
        };

我的MVC控制器方法

 public void GenerateAgreedFlow()
        {
            var planService = ServiceFactory.PlanService;

            var fileName = "ExcellData.xlsx";
            var file = new FileInfo( fileName);


            using (var package = new OfficeOpenXml.ExcelPackage(file))
            {
                // add a new worksheet to the empty workbook
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("AgreedPlan " + DateTime.Now.ToShortDateString());

                // --------- Data and styling goes here -------------- //

                DataTable dt =planService.GetAgreedYarnFlow();
                int iCol = 1;
                // Add column headings...

                foreach (DataColumn c in dt.Columns)
                {
                    worksheet.Cells[1, iCol].Value = c.ColumnName;
                    worksheet.Cells[1, iCol].Style.Fill.PatternType = ExcelFillStyle.Solid;
                    worksheet.Cells[1, iCol].Style.Fill.BackgroundColor.SetColor(Color.LightGray);
                    iCol++;
                }




                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    for (int k = 0; k < dt.Columns.Count; k++)
                    {

                        worksheet.Cells[j + 2, k + 1].Value = dt.Rows[j].ItemArray[k].ToString();



                        if (int.Parse(dt.Rows[j].ItemArray[7].ToString()) == 1)
                        {
                            worksheet.Cells[j + 2, k + 1].Style.Locked = false;
                            worksheet.Cells[j + 2, k + 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
                            worksheet.Cells[j + 2, k + 1].Style.Fill.BackgroundColor.SetColor(Color.LightBlue);
                        }
                    }

                    var colCount = dt.Columns.Count;

                }


                worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();
                worksheet.Column(1).Hidden = true;
                worksheet.Column(2).Hidden = true;
                worksheet.Column(3).Hidden = true;
                worksheet.Column(4).Hidden = true;
                worksheet.Column(5).Hidden = true;
                worksheet.Column(8).Hidden = true;
                worksheet.Protection.IsProtected = true;
                // save our new workbook and we are done!
                package.Workbook.Properties.Title = "Attempts";
                this.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                this.Response.AddHeader(
                          "content-disposition",
                          string.Format("attachment;  filename={0}", "ExcellData.xlsx"));
                this.Response.BinaryWrite(package.GetAsByteArray());
            }

有没有办法从ajax响应中下载文件?

0 个答案:

没有答案