对使用 ajax 下载二进制文件的支持不是很好,它作为工作草案仍在开发中。
简单的下载方法:
您只需使用下面的代码即可让浏览器下载请求的文件,并且所有浏览器都支持这种方式,并且显然会以相同的方式触发 WebApi 请求。
$scope.downloadFile = function(downloadPath) { 
    window.open(downloadPath, '_blank', '');  
}
Ajax二进制下载方法:
可以在某些浏览器中使用 ajax 下载二进制文件,下面是一个适用于最新版本的 Chrome、Internet Explorer、FireFox 和 Safari 的实现。
它使用arraybuffer响应类型,然后将其转换为 JavaScript blob,然后使用该saveBlob方法呈现以保存- 尽管目前仅在 Internet Explorer 中存在 - 或转换为由浏览器打开的 blob 数据 URL,触发下载对话框(如果支持在浏览器中查看 mime 类型)。
Internet Explorer 11 支持(已修复)
注意:Internet Explorer 11 不喜欢使用msSaveBlob别名的功能 - 可能是安全功能,但更可能是缺陷,因此var saveBlob = navigator.msSaveBlob || navigator.webkitSaveBlob ... etc.用于确定可用saveBlob支持导致异常;因此为什么下面的代码现在navigator.msSaveBlob单独测试。谢谢?微软
// Based on an implementation here: web.student.tuwien.ac.at/~e0427417/jsdownload.html
$scope.downloadFile = function(httpPath) {
    // Use an arraybuffer
    $http.get(httpPath, { responseType: 'arraybuffer' })
    .success( function(data, status, headers) {
        var octetStreamMime = 'application/octet-stream';
        var success = false;
        // Get the headers
        headers = headers();
        // Get the filename from the x-filename header or default to "download.bin"
        var filename = headers['x-filename'] || 'download.bin';
        // Determine the content type from the header or default to "application/octet-stream"
        var contentType = headers['content-type'] || octetStreamMime;
        try
        {
            // Try using msSaveBlob if supported
            console.log("Trying saveBlob method ...");
            var blob = new Blob([data], { type: contentType });
            if(navigator.msSaveBlob)
                navigator.msSaveBlob(blob, filename);
            else {
                // Try using other saveBlob implementations, if available
                var saveBlob = navigator.webkitSaveBlob || navigator.mozSaveBlob || navigator.saveBlob;
                if(saveBlob === undefined) throw "Not supported";
                saveBlob(blob, filename);
            }
            console.log("saveBlob succeeded");
            success = true;
        } catch(ex)
        {
            console.log("saveBlob method failed with the following exception:");
            console.log(ex);
        }
        if(!success)
        {
            // Get the blob url creator
            var urlCreator = window.URL || window.webkitURL || window.mozURL || window.msURL;
            if(urlCreator)
            {
                // Try to use a download link
                var link = document.createElement('a');
                if('download' in link)
                {
                    // Try to simulate a click
                    try
                    {
                        // Prepare a blob URL
                        console.log("Trying download link method with simulated click ...");
                        var blob = new Blob([data], { type: contentType });
                        var url = urlCreator.createObjectURL(blob);
                        link.setAttribute('href', url);
                        // Set the download attribute (Supported in Chrome 14+ / Firefox 20+)
                        link.setAttribute("download", filename);
                        // Simulate clicking the download link
                        var event = document.createEvent('MouseEvents');
                        event.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
                        link.dispatchEvent(event);
                        console.log("Download link method with simulated click succeeded");
                        success = true;
                    } catch(ex) {
                        console.log("Download link method with simulated click failed with the following exception:");
                        console.log(ex);
                    }
                }
                if(!success)
                {
                    // Fallback to window.location method
                    try
                    {
                        // Prepare a blob URL
                        // Use application/octet-stream when using window.location to force download
                        console.log("Trying download link method with window.location ...");
                        var blob = new Blob([data], { type: octetStreamMime });
                        var url = urlCreator.createObjectURL(blob);
                        window.location = url;
                        console.log("Download link method with window.location succeeded");
                        success = true;
                    } catch(ex) {
                        console.log("Download link method with window.location failed with the following exception:");
                        console.log(ex);
                    }
                }
            }
        }
        if(!success)
        {
            // Fallback to window.open method
            console.log("No methods worked for saving the arraybuffer, using last resort window.open");
            window.open(httpPath, '_blank', '');
        }
    })
    .error(function(data, status) {
        console.log("Request failed with status: " + status);
        // Optionally write the error out to scope
        $scope.errorDetails = "Request failed with status: " + status;
    });
};
用法:
var downloadPath = "/files/instructions.pdf";
$scope.downloadFile(downloadPath);
笔记:
您应该修改 WebApi 方法以返回以下标头:
我希望这有帮助。