【问题标题】:Display pdf using angularjs使用angularjs显示pdf
【发布时间】:2017-02-09 14:50:49
【问题描述】:

我正在尝试从服务器下载 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);
    }

【问题讨论】:

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


    【解决方案1】:

    看看here 的建议,或者你可以尝试一个专门用于显示 pdf 的 npm 模块,such as this one.

    【讨论】:

      【解决方案2】:

      好的,我找到了解决方案。需要稍微修改一下我的 Angular 代码,它运行良好。如果有人遇到同样的问题,这里是代码:

       function loadDocument(fileName) {
      
              REST.post(commonService.constants.webapi.helpFileStreamKey + 'firmUserGuid')
            .then(function(response) {
                var streamGuid = response.data;
      
                REST.get(commonService.constants.webapi.helpReferenceGuide + streamGuid).then(function (response) {
                    var pdfFileURL = response.config.url;
                    $window.open(pdfFileURL);
      
                });
      
                  })
              .catch(function (e) { $scope.errorHandler(moduleName, e); })
              .finally($scope.waitOn);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-08
        • 2015-04-23
        • 2014-03-04
        • 2014-12-22
        • 1970-01-01
        • 2020-05-31
        • 2014-05-13
        • 2013-01-11
        相关资源
        最近更新 更多