从响应中填充角度模型

时间:2016-09-27 18:28:57

标签: angularjs asp.net-mvc-4

我得到了像

这样的回复
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.ByteArrayContent, Headers:
{
  x-filename: StatementofAccount.pdf
  Content-Type: application/octet-stream
  Content-Disposition: attachment; filename=StatementofAccount.pdf
}

现在我需要使用" x-filename:StatementofAccount.pdf"但我无法取得这个。 我把这个回复填写到我的模型中  $ scope.dataDetail = response.data; 但是当我试着去的时候                 的console.log($ scope.dataDetail.StatusCode)

或 的console.log($ scope.dataDetail.Headers.x-文件名)

但显示未定义。 请告诉我一个我在做错的地方或者如何实现这个目标。

我尝试了以下方式..plz让我知道我哪里错了.....

c#
....

byte[] bytes = memoryStream.ToArray();
                    memoryStream.Close();
httpResponseMessage.Content = new ByteArrayContent(bytes.ToArray());
                    httpResponseMessage.Content.Headers.Add("x-filename", fileName);
                    httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                    httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
                    httpResponseMessage.Content.Headers.ContentDisposition.FileName = fileName;
                    httpResponseMessage.StatusCode = HttpStatusCode.OK;
                    return httpResponseMessage;
中的

 $http({
            method: 'post',
            url: basePath + '/profile/downloadpdf_fromedit',
           // data: JSON.stringify($scope.paginginfostmntaccnt),
            responsetype: 'arraybuffer',
            headers: {'content-type': 'application/pdf'},
            //  headers: { 'content-type': 'application/json' }
        })
            .then(function (response) {
              //  console.log(response.data);

                $scope.dataDetail = response.data;
                console.log($scope.dataDetail)

 1.               var file = new Blob([response.data], { type: 'application/pdf' });

 saveAs(file, 'StatementofAccount.pdf');

//url-file:///C:/Users/tushar/Downloads/StatementofAccount.pdf
//failed to load pdf

2.var file = new Blob([response], { type: 'application/pdf' });

  var fileurl = URL.createObjectURL(file);
                window.open(fileurl);

//url- blob:http://localhost:16311/02f8d85e-74c0-4ccd-b937-22f02cc3866c

//failed to load pdf document


3.
.success(function (data, status, headers, config) {
            // any required additional processing here 
            var results = [];
            results.data = data;
            results.headers = headers();
            results.status = status;
            results.config = config;

            console.log(results)

                $("#loading").hide();
                headers = headers();

                var filename = headers['x-filename'];

                var contentType = headers['content-type'];
                if (!filename) {
                    filename = headers["x-filename"] || 'statementAccount.pdf';
                }

                var linkElement = document.createElement('a');
                try {
                    var blob = new Blob([data], { type: contentType });
                    var url = window.URL.createObjectURL(blob);

                    linkElement.setAttribute('href', url);
                    linkElement.setAttribute("download", filename);

                    var clickEvent = new MouseEvent("click", {
                        "view": window,
                        "bubbles": true,
                        "cancelable": false
                    });
                    linkElement.dispatchEvent(clickEvent);
                    $("#loading").hide();

                    console.log("filename" + filename);

                } catch (ex) {
                    console.log("catch"+ex);
                    $("#loading").hide();
                }
            })
        //url-file:///C:/Users/tushar/Downloads/statementAccount.pdf
//failed to load pdf document

1 个答案:

答案 0 :(得分:0)

要访问响应的标头,您需要使用response.headers而不是response.data,数据部分包含响应的正文。