Javascript 使用AngularJS导出和下载文件:错误关于:blank#blocked
我正在使用AngularJs和C#开发一个应用程序,该应用程序具有导出和下载文件.xls的功能,但当我单击下载文件的按钮时,浏览器的新选项卡中的响应是“about:blank#blocked” HTML代码:Javascript 使用AngularJS导出和下载文件:错误关于:blank#blocked,javascript,angularjs,Javascript,Angularjs,我正在使用AngularJs和C#开发一个应用程序,该应用程序具有导出和下载文件.xls的功能,但当我单击下载文件的按钮时,浏览器的新选项卡中的响应是“about:blank#blocked” HTML代码: <button class="btn btn-success" ng-click="exportInfo()"> <i class="fa fa-file-excel-o"></i>Export </button> $scope.$
<button class="btn btn-success" ng-click="exportInfo()">
<i class="fa fa-file-excel-o"></i>Export
</button>
$scope.$watch('info',函数(newValue,oldValue){
如果(newValue!=未定义&&newValue.length>0){
$scope.sleep=函数(时间){
返回新承诺(函数(解析){setTimeout(解析,时间);});
};
angular.element(“#infoTable”).ready(函数(){
$scope.sleep(500)。然后(函数(){
//延迟后在此处运行的代码
var exportHref=Excel.tableToExcel(“#infoTable”,“info”);
window.open(exportHref);
});
});
}
});
应用程序工厂('Excel',函数($window){
var uri='data:application/vnd.ms excel;base64',
template='出于好奇。是否尝试在单击处理程序上添加preventDefault?出于好奇。是否尝试在单击处理程序上添加preventDefault?
$scope.exportInfo = function () {
if ($route.current.loadedTemplateUrl.indexOf('Index.html') > 0) {
InfoService.getInfo().then(function (e) {
$scope.info = angular.fromJson(e.data);
});
}
};
$scope.$watch('info', function (newValue, oldValue) {
if (newValue != undefined && newValue.length > 0) {
$scope.sleep = function (time) {
return new Promise(function (resolve) { setTimeout(resolve, time); });
};
angular.element("#infoTable").ready(function () {
$scope.sleep(500).then(function () {
//code to run here after the delay
var exportHref = Excel.tableToExcel('#infoTable', 'info');
window.open(exportHref);
});
});
}
});
app.factory('Excel', function ($window) {
var uri = 'data:application/vnd.ms-excel;base64,',
template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"><head <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
base64 = function (s) { return $window.btoa(unescape(encodeURIComponent(s))); },
format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) };
return {
tableToExcel: function (tableId, worksheetName) {
var table = $(tableId),
ctx = { worksheet: worksheetName, table: table.html() },
href = uri + base64(format(template, ctx));
return href;
}
};
});