加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 带有blob和WebAPI的Angular FileSaver.下载的PDF是

发布时间:2020-12-17 17:27:44 所属栏目:安全 来源:网络整理
导读:这是我用于返回包含多个图像的PDF的API.现在,当我使用url调用它时,它可以完美地下载带有图像的PDF.例如两页带图像. [HttpGet] public async TaskIHttpActionResult Download(Guid customDocId) { byte[] responseContent = await Task.FromResult(FileNetApi
这是我用于返回包含多个图像的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);

            }

现在从角度我使用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 () {

            });
        }

和服务:

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

有没有人知道为什么在使用FileSaver保存时返回空白页面,而直接下载则对所有内容都完全没问题.

解决方法

我不得不改变Restangular中的某些东西.这是responseType必须是arrayBuffer或’blob’.我没有明确尝试过arrayBuffer. blob响应类型对我有用. restangular中缺少配置.所以我对我的服务做了一些改变,瞧!它工作正常.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读