当在HttpResponseMessage中发送并以角度承诺

时间:2015-11-12 10:32:17

标签: c# angularjs asp.net-web-api

我遇到了通过电汇传输信息的问题,让我感到困惑。

我有一个WebAPI端点。当被调用时,它要求提供特定文件,然后将其作为bytearraycontent

提供
public class SampleType
{
    public string Url { get; set; }
    public string Id { get; set; }
}

[HttpPost]
[Route("document")]
public async Task<HttpResponseMessage> BuildDocumentAsync(HttpRequestMessage request, SampleType candidateInput)
{
    var document = await _applicationService.GetByIdAsync(candidateInput.Id);
    var generatedDocument = new CandidatePdfDocumentBuilder().BuildDocument(document, candidateInput.Url);

    var response = request.CreateResponse(HttpStatusCode.OK);
    response.Content = new ByteArrayContent(generatedDocument);
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

    return response;
}

我可以看到这里构建的文档的大小在我的测试用例中为53993字节。这是一份PDF文件。我在测试用例中将文档写入磁盘,磁盘上文件的大小与我generatedDocument中的字节数一致。

我有一个与之沟通的角度架构。

包装$http

的基础架构服务
(function () {
    "use strict";
    var httpModelService = function ($http) {

        this.postTo = function (endpoint, model) {
            return $http.post(endpoint, model);
        }
    };

    var app = angular.module("myApplicationName");
    app.service("httpModelService", httpModelService);
}());

服务

this.buildDocument = function (documentID) {
    return httpModelService.postTo('release-protocol/document', { url: $location.absUrl(), id: documentID });
};

控制器

releaseProtocolService.buildDocument($scope.document.id).
    then(function(response) {
        var dataSize = response.data.length;
            saveData(response.data, $scope.document.documentName + '.pdf');
        });

但是,我可以看到,如果我检查response.data的长度,在我的情况下,这是一个较小的字节数 - 在我的情况下为52137。

有了所有这些背景,可能导致我的response.data中可用字节数减少的原因是什么?

1 个答案:

答案 0 :(得分:0)

Response.data.length gives you the character length not necessarily the byte size.