2017-02-09 14 views
1

サーバからpdfファイルをダウンロードしてブラウザに表示しようとしています。私が持っているWeb APIのメソッドは、httpResponseMessageとしてファイルを返し、それはファイルを返すので正常に動作しています。しかしAngularJs側では私はファイルを表示することができません。誰かが私が何が欠けているのか理解できるように助けることができる?angularjsを使用してpdfを表示

ウェブアピ方法:

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); 
    } 

答えて

0

私は解決策を見つけました。角度コードを少し変更する必要があり、うまくいきました。誰かが同じ問題に直面している場合のコードは次のとおりです。

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); 
    } 
0

提案hereを見てみましょうか、あるいはどここのPDFを表示するために特別にNPMモジュールを試すことができます既にあなたの世話をしています。such as this one.

関連する問題