javascript - 如何处理 ASP .NET Api HTTPResponseMessage 来下载文件

标签 javascript asp.net angularjs ajax asp.net-web-api

我正在阅读如何从 asp.net api 下载文件的解决方案: https://stackoverflow.com/a/3605510/1881147

因此我创建了一个 API 处理程序,代码如下:

public HttpResponseMessage Post([FromBody]dynamic result)
        {

            var localFilePath = graphDataService.SaveToExcel(graphVm, graphImgUrl);
            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
            response.Content = new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read));
            response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
            response.Content.Headers.ContentDisposition.FileName = "testing.xlsx";
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("MS-Excel/xls");

            return response;
            //return graphDataService.SaveToExcel(graphVm, graphImgUrl);
        }

这是我的客户端:

     $http({
            url: '/msexcel',
            method: 'post',
            params: { param: JSON.stringify(param) }
        }).success(function (data, status, headers, config) {
            console.log(data); //HOW DO YOU HANDLE the response here so it downloads?
        }).error(function (data, status, headers, config) {
            console.log(status);
        });

如何处理成功,以便将文件下载为 .xls 文件?谢谢

最佳答案

不要将响应内容类型设置为 MS-Excel/xls,而是使用 application/octet-stream。

public HttpResponseMessage GetFile()
{
    var localFilePath = graphDataService.SaveToExcel(graphVm, graphImgUrl);
    HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
    response.Content = new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read));
    response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
    response.Content.Headers.ContentDisposition.FileName = "testing.xlsx";
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
    response.Content.Headers.Add("x-filename", "testing.xlsx"); //We will use this below
    return response;
}

棘手的部分是如何从成功回调中的响应中强制下载。 一种解决方案是根据响应数据创建一个 blob,然后再次下载该 blob。 在 IE 中,我们可以保存该 blob,但对于大多数浏览器,我们需要欺骗浏览器

$http({
    method: 'GET',
    url: Url,
    responseType: 'arraybuffer',
    headers: {
        'Authorization': Token,
        'Access-Control-Allow-Origin': '*',
    }
}).success(function (data, status, headers) {
    headers = headers();

    var filename = headers['x-filename'];
    var contentType = headers['content-type'];

    try {
        var blob = new Blob([data], { type: contentType });

        //Check if user is using IE
        var ua = window.navigator.userAgent;
        var msie = ua.indexOf("MSIE ");

        if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))
        {
            window.navigator.msSaveBlob(blob, filename);
        }
        else  // If another browser, return 0
        {
            //Create a url to the blob
            var url = window.URL.createObjectURL(blob);
            var linkElement = document.createElement('a');
            linkElement.setAttribute('href', url);
            linkElement.setAttribute("download", filename);

            //Force a download
            var clickEvent = new MouseEvent("click", {
                "view": window,
                "bubbles": true,
                "cancelable": false
            });
            linkElement.dispatchEvent(clickEvent);
        }

    } catch (ex) {
        console.log(ex);
    }
}).error(function (message) {
    console.log(message);
});

注意:*a 标签上的下载属性仅在 HTML5 中受支持。 *我通过返回 ByteArrayContent 来完成这项工作,但 StreamContent 也应该像它一样工作 also返回二进制数据。

解决方案基于this很棒的文章,但我在此解决方案中包含了对 IE 的支持

关于javascript - 如何处理 ASP .NET Api HTTPResponseMessage 来下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41114870/

相关文章:

javascript - 使用 jQuery 淡出 HTML-Table 中的列 : Why is . fadeTo() 这么慢?

javascript - 如何将环境变量转换为JSON对象?

asp.net - ASP .NET 页面名称 "alias"

c# - ASP.NET WindowsAuthentication 自定义 401 未授权错误页面

ASP.NET 子窗口在每次回发时都会在新窗口上打开

javascript - 从 Angular $http 获取 promise 数据

javascript - 指令范围

javascript - 谷歌地图 map 不显示?

javascript - 当网格底部有空白时,Bootstrap + Masonry 添加空白 div

javascript - Angularjs,无法从数据库返回的 json 对象设置下拉列表