相关疑难解决方法(0)

使用AngularJS从ASP.NET Web API方法下载文件

在我的Angular JS项目中,我有一个<a>锚标记,当单击它时会GET向返回文件的WebAPI方法发出HTTP 请求.

现在,我希望在请求成功后将文件下载到用户.我怎么做?

锚标记:

<a href="#" ng-click="getthefile()">Download img</a>
Run Code Online (Sandbox Code Playgroud)

AngularJS:

$scope.getthefile = function () {        
    $http({
        method: 'GET',
        cache: false,
        url: $scope.appPath + 'CourseRegConfirm/getfile',            
        headers: {
            'Content-Type': 'application/json; charset=utf-8'
        }
    }).success(function (data, status) {
        console.log(data); // Displays text data if the file is a text file, binary if it's an image            
        // What should I write here to download the file I receive from the WebAPI method?
    }).error(function (data, status) {
        // ...
    });
}
Run Code Online (Sandbox Code Playgroud)

我的WebAPI方法: …

html javascript c# asp.net-web-api angularjs

132
推荐指数
3
解决办法
19万
查看次数

标签 统计

angularjs ×1

asp.net-web-api ×1

c# ×1

html ×1

javascript ×1