【问题标题】:Download a file using angularjs, asp.net使用 angularjs、asp.net 下载文件
【发布时间】:2015-07-06 19:59:38
【问题描述】:

我正在尝试通过单击按钮下载一个 excel 文件(动态生成)。当代码放置在服务器端 (Reports.aspx.cs) 并且在按钮单击时有回发时,该功能可以正常工作。 但是,现在,前端技术是 Angular。所以没有回传。尝试在处理程序中使用相同的下载代码并且下载不会发生。没有保存提示,没有错误。断点会命中 handler.cs。

Reports.aspx:

<button type="button" data-ng-click="DownloadExcelReport()">Download Report</button>

ReportsCtrl.js --controller

$scope.DownloadExcelReport = function () {        
        ReportsFactory.DownloadReport($scope.ReportId,$scope.SetId);       
    }

ReportsFactory.js --service

factory.DownloadReport = function (reportId, setId) {
return $http({
   url: "http://localhost:62102/download.ashx?reportId=" + reportId + "&setId=" + setId,
            method: "GET"            
        }).success(function (data, status) {
        }).error(function (data, status) {
        });
}

download.ashx.cs --handler

public void ProcessRequest(HttpContext context)
    {

        int reportId = Convert.ToInt32(context.Request.QueryString["reportId"]);
        int setId = Convert.ToInt32(context.Request.QueryString["setId"]);            
        switch (reportId)
        {
            case 1:
                DataTable dt = GetData(reportId, setId);
                if (dt != null)
                {

                    string FileName = "Responses";

                    ExportExcel obj = new ExportExcel();
                    obj.showGridLines = true;
                    obj.headerStyle = new Style(Color.SlateGray, Color.White, Color.SlateGray, ExcelBorderStyle.Thin);
                    MemoryStream ms = obj.GenerateDocument(dt);
                    HttpContext.Current.Response.Clear();
                    HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + FileName + ".xlsx" + "\"");
                    HttpContext.Current.Response.BinaryWrite(ms.ToArray());
                    HttpContext.Current.Response.Flush();
                    HttpContext.Current.Response.End();                        
                }
                break;

        }
    }

编辑:

后来我被教导,当使用Javascript下载时,方法是不同的。你创建一个表单,然后提交带有参数的表单。我已经添加了工作解决方案。

这可能对某人有所帮助。

【问题讨论】:

  • 不发表评论就投反对票无济于事。
  • 你能把代码的相关部分贴在你说可以正常工作的地方吗?另外,你说的下载没有发生是什么意思?你得到一个零字节文件吗?你有404吗?你至少得到一个Save As 消息框吗?肯定发生了某事吗?你怎么称呼你的处理程序?您使用的是ashx 文件吗?你也可以发一下吗?谢谢。
  • 对改进此解决方案的任何建议表示赞赏。

标签: c# asp.net angularjs


【解决方案1】:

ReportsFactory.js --service

factory.DownloadReport = function (reportId, setId) {
        var form = document.createElement("form");
        form.action = "http://localhost:62102/download.asmx/DownloadReport";
        form.method = "POST";
        form.target = "_self";
        var input = document.createElement("input");
        input.type = "text";
        input.name = "params";
        input.value = reportId + "," + setId;
        form.appendChild(input);        
        form.style.display = 'none';
        document.body.appendChild(form);
        form.submit();        
    };

现在使用 asmx 文件而不是处理程序。

download.asmx.cs

[WebMethod]
public void DownloadReport()
{
   string[] Params = Convert.ToString(HttpContext.Current.Request.Form["params"]).Split(',');
   string FileName = "Reports_";
   int reportId = Convert.ToInt32(Params[0]);
   int setId = Convert.ToInt32(Params[1]);
   DataTable dt = GetData(reportId,setId);
   ExportExcel obj = new ExportExcel();
   MemoryStream ms = obj.GenerateDocument(dt);
   HttpContext.Current.Response.Clear();
   HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
   HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + FileName + ".xlsx" + "\"");
   HttpContext.Current.Response.BinaryWrite(ms.ToArray());
   HttpContext.Current.Response.Flush();
   HttpContext.Current.Response.End();   
}

【讨论】:

    猜你喜欢
    • 2017-03-06
    • 2014-07-27
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2017-01-24
    相关资源
    最近更新 更多