带有 blob 和 WebAPI 的 Angular FileSaver。下载的PDF是空白的。但是 API url 返回带有内容的 PDF

Joy*_*Joy 6 asp.net-web-api angularjs filesaver.js

这是我用于返回包含多个图像的 PDF 的 API。现在,当我用 url 调用它时,它会完美地下载带有图像的 PDF。例如,带有图像的两页。

 [HttpGet]

    public async Task<IHttpActionResult> Download(Guid customDocId)
    {

                    byte[] responseContent = await Task.FromResult(FileNetApiClientFactory.Get(customDocId).DownloadDocument(customDocId, "pdf", true));
                    HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK)
                    {
                        Content = new ByteArrayContent(responseContent),
                        StatusCode = HttpStatusCode.OK,
                    };
                    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = string.Concat(customDocId.ToString(), ".pdf") };
                    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
                    return ResponseMessage(response);

            }
Run Code Online (Sandbox Code Playgroud)

现在从 angular 我使用 blob 并FileSaver保存 PDF 。所以当我下载它时。然后它只返回两页但没有内容。但它显示第 1 页和第 2 页,但它们是空白的。

这是我的角度代码:

 //saveAs method is from FileSaver.js
        vm.download = function () {
            documentService.download($scope.customDocumentId).then(function (fileData) {
                var blob = new Blob([fileData], { type: 'application/pdf' });
                saveAs(blob, $scope.customDocumentId + ".pdf");
            }).catch(function () {

            });
        }
Run Code Online (Sandbox Code Playgroud)

和服务:

function _download(customDocumentId) {
            return Restangular
                .one('customdocument', customDocumentId).one('download')
                .get(null, { responseType: 'arraybuffer' });
        }
Run Code Online (Sandbox Code Playgroud)

有没有人知道为什么在使用 FileSaver 保存时返回空白页,而直接下载它对所有内容都很好。

Joy*_*Joy 3

我必须改变 Restangular 中的某些内容。这是responseType必须的arrayBuffer或“一团”。我还没有明确尝试使用 arrayBuffer 。blob 响应类型对我有用。矩形中缺少配置。所以我对我的服务做了一些改变,瞧!它正在工作。

所以更新后的服务现在看起来像这样。DocumentServicesRestangular只不过是一个工厂包装器,通过RestangularConfigurer更改了baseurl。

   function _download(customDocumentId) {
            return DocumentServicesRestangular.one('customdocument', customDocumentId).one('download')
                    .withHttpConfig({ responseType: 'blob' }).get();
        }
Run Code Online (Sandbox Code Playgroud)