$ http.get没有所有标头

时间:2017-11-17 09:06:14

标签: javascript angularjs asp.net-web-api2

我试图从我的web-api控制器下载文件。我成功了,直到我想获得响应的自定义标题。

web-api控制器

[HttpGet]
[Route("vorlage")]
[ResponseType(typeof(object))]
[SwaggerResponse(HttpStatusCode.OK, Type = typeof(object))]
public HttpResponseMessage DownloadVorlage()
{
    HttpResponseMessage result = Request.CreateResponse(HttpStatusCode.OK);
    result.Content = new ByteArrayContent(Properties.Resources.Vorlage_Batch);
    result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
    result.Content.Headers.ContentDisposition.FileName = "original name.xlsx";
    result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    result.Headers.Add("X-Filename", "original name.xlsx");
    return result;
}

angularjs代码

function DownloadVorlage($scope, $http, WebApiBaseUri) {
    var request = {
        method: 'GET',
        url: WebApiBaseUri + 'batchmode/vorlage',
        responseType: 'arraybuffer',
        headers: {
            'Accept': 'application/json;odata=verbose'
        }
    };
    $http(request).then(function (response) {
        var headers = response.headers();
        var filename = headers['X-Filename'] || 'replacement name.xlsx';
        var contentType = headers['content-type'] || 'application/octet-stream';
        download(response.data, filename, contentType);
    }, function (response) {
        console.log(response);
    });
}

内容类型正确,可以保存文件。 当我用邮递员测试API时,我得到了我需要的所有标题信息。 但我去的所有标题信息都是:

cache-control: "no-cache"
content-type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
expires: "-1"
pragma: "no-cache"

我错过了" X-Filename"信息,并不知道如何获得它。

0 个答案:

没有答案