使用Angular Js自动下载pdf文件

时间:2015-07-28 13:56:31

标签: angularjs pdf filesaver.js

我有一个使用WEB API制作的API,它使用类型作为HttpResponseMessage提供pdf文件。

此API是从Web应用程序调用的,该应用程序使用Angular Js。

我的问题是,调用此API时无法下载文件。它给了我损坏的文件。

这是我的WebAPI代码

            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
            byte[] fileBytes = System.IO.File.ReadAllBytes(outputFilePath);
            response.Content = new ByteArrayContent(fileBytes);
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
            //response.Content.Headers.ContentDisposition.FileName = "Certificate#" + ideaModel.IdeaNumber + ".pdf";
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

            return new CertificateModel { file = response, fileName = "Certificate#" + ideaModel.IdeaNumber.ToString()+".pdf" };

在angular JS中,我正在调用URL,并尝试使用FileSaver.js

保存文件
  $http.post(getUrl('api/Reward/DeliverGift'),
        {
            IdeaNumber: '4',
            SubmittedBy: 'Self',
        },{responseType: 'arraybuffer'}
    ).then(function (response) {            
        var certificateFile = new Blob([response.data.file], { type: 'application/pdf' });
        saveAs(certificateFile, response.data.fileName );

    }, function (errors) {         
    });

我的原始文件是195Kb,但是上面的代码给了我一个9字节的文件

0 个答案:

没有答案
相关问题