javascript - 使用 angularjs 显示 pdf

标签 javascript c# angularjs pdf asp.net-web-api

我正在尝试从服务器下载 pdf 文件并在浏览器中显示它。我的 web api 方法将文件作为 httpResponseMessage 返回,并且工作正常,因为它返回文件。但在 AngularJs 端我无法显示该文件。有人可以帮助我理解我错过了什么吗?

Web Api 方法:

  public HttpResponseMessage GetHelpReferenceDocs(Guid streamKey)
    {
        var fakeFileName = GetStream(streamKey); // If this succeeds, stream is known and unexpired.

        // Internal file name
        string staticFileName = helpFiles[fakeFileName];

        var mappedPath = System.Web.Hosting.HostingEnvironment.MapPath("~/Static/" + staticFileName);

        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
        var stream = new FileStream(mappedPath, FileMode.Open, FileAccess.Read, FileShare.Read);

        result.Content = new StreamContent(stream);
        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
        result.Content.Headers.ContentDisposition.FileName = Path.GetFileName(mappedPath);
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
        result.Content.Headers.ContentLength = stream.Length;
        return result;


    }

AngularJS:

  function loadDocument(fileName) {

        REST.post(commonService.constants.webapi.helpFileStreamKey + fileName)
      .then(function(response) {
          var streamGuid = response.data;

          REST.get(commonService.constants.webapi.helpReferenceGuide + streamGuid).then(function (response) {

             $window.open(response.data);

          });

            })
        .catch(function (e) { $scope.errorHandler(moduleName, e); })
        .finally($scope.waitOn);
    }

最佳答案

看看建议 here ,或者您可以尝试专门用于显示 pdf 的 npm 模块,该模块已经为您处理好了,such as this one.

关于javascript - 使用 angularjs 显示 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42139795/

相关文章:

angularjs - Nodejs 中 Mongodb 的查找

javascript - 只有第一个单词的大写首字母

javascript - 有没有办法在 firefoxQuantum 中使用 tabs.saveAsPDF() 而不使用对话框?

javascript - 为什么我不能在不使用字符串的情况下将 Dog Face (u+1f436) 字段添加到我的对象?

c# - Excel:获取 Excel 中周围的命名范围

c# - WPF 'Set property ' System.Windows.ResourceDictionary.DeferrableContent' 抛出异常。'

javascript - 根据Angular中的ng-repeat索引查找数组项

c# - response.redirect 并抛出 catch block

javascript - 数组未绑定(bind)到使用 AngularJs 选择

javascript - 如何在 ng-repeat 中使用 ng-switch 来编辑 JSON API 数据