2017-03-02 70 views
1

目标:显示pdf文件,作为服务器在新窗口中使用烬的响应收到。无法在浏览器中使用余烬阅读pdf内容

代码在路由文件:

actions: { 
    pdfClick(assessmentId) { 
     var result = []; 
     return new Ember.RSVP.Promise(function (resolve, reject) { 
     Ember.$.ajax({ 
      type: 'GET', 
      url: 'http://localhost:4200/abc/secured/rest/questions/166', 
      success: function (data) { 
      var winlogicalname = "detailPDF"; 
      var winparams = 'dependent=yes,locationbar=no,scrollbars=yes,menubar=yes,' + 
       'resizable,screenX=50,screenY=50,width=850,height=1050'; 

      var detailWindow = window.open("content-type:application/pdf"); 
      detailWindow.document.write(data); 
      }, 
      error: function (request, textStatus, error) { 
      console.log(error); 
      } 
     }); 
     }); 
    } 
    } 

困惑:

var detailWindow = window.open("content-type:application/pdf"); 
detailWindow.document.write(data); 

在window.open,我们设置内容类型为application/PDF,然后我们什么时候尝试写入数据(从服务器收到的PDF文件的字节流),垃圾数据出现在新窗口中。

如果我通过Chrome浏览器访问该服务,Chrome能够显示PDF,但如果我使用ember ajax访问相同的服务,我无法在新窗口中显示PDF。

回答

0

问题是关于jQuery。 jQuery不处理二进制数据(Reference)。当你的服务器发送二进制文件时,你不能处理这个请求。

你有不同的选择:

  1. 使用Base64加密运输二进制数据:其实我不喜欢它,因为它的开销。
  2. 使用纯XMLHttpRequest而不是jQuery:我已经在我当前的项目中使用了它,它的工作原理!但是代码太多,很难追踪和维护。需要包装Ember.Promise。我的实现非常接近这些答案:12
  3. 使用jQuery传输器:我看到它,但还没有使用它。我不知道它是否工作。如果你想看,这里是reference