AngularJS - 显示加载图标,直到下载csv文件

时间:2014-06-26 11:46:13

标签: javascript angularjs csv asp.net-web-api

我有AngularJS应用程序,我发送到服务器上下载svc文件,如下所示:

public static void DownloadCSV(string csvFile, string fileName)
    {
        HttpContext.Current.Response.AppendHeader("Content-Disposition",
             "attachment; " +
             "filename=" + fileName + ".csv;" +
             "creation-date=" + DateTime.Now.ToString() + "; " +
             "modification-date=" + DateTime.Now.ToString() + "; " +
             "read-date=" + DateTime.Now.ToString());
        HttpContext.Current.Response.ContentType = "text/csv";
        HttpContext.Current.Response.Write(csvFile);
        HttpContext.Current.Response.End();
    }

在客户端我发送到一个名为“exportToCsv”的webApi函数,它使用DownloadCSV函数,这样:

            var p = document.createElement('a')
            p.setAttribute('href', "myApp/exportToCsv" )
            p.click();

我的问题是我想向用户提供一个加载图标,直到文件完成下载。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

准备一个带有加载图标(可能是.gif)的html,该图标位于屏幕中心,其余部分可能会被遮蔽;

<div ng-show="loading" ng-include src="'loading.html'" ></div>

在JS方面,在下载开始之前,将loading设置为true,完成后将其设置为false

$scope.loading = false; // when starts
$scope.loading = false; // when finished