2016-03-24 171 views
0

我试图在IE中以角度+节点设置开始下载时遇到问题。以编程方式从回调中以编程方式开始下载在IE

首先,这里是我当前的进程:

  • 用户点击下载按钮
  • 它要求将文件从节点服务器
  • 节点服务器生成的文件,并将其发送回JS控制器
  • 的JS控制器生成该文件的URL,并将其作为一个下载

版本我一直使用到现在看起来是这样的(简化清楚起见):

var self = this; 
var invisibleLink = document.getElementById('invisible-link'); 

this.services.http.post("createPdf", {foo: 'bar}, {responseType: 'arraybuffer'}) 
    .then(function(response){ 
    var file = new Blob([response.data], {type: 'application/pdf'}); 
    var fileURL = window.URL.createObjectURL(file); 
    invisibleLink.href = fileURL; 
    invisibleLink.download = 'myDownloadedFile'; 
    invisibleLink.click(); 
    }); 

这种运作良好,它的顺利,一切都在... FF和铬。在IE(11)中,我收到“错误:访问被拒绝”。

显然.download和/或.click()不被接受,所以下面的其他SE问题,我试图使用window.location。不幸的是,这似乎也不起作用。

下面是我试过了,结果:

window.location.href = fileURL; 
// Error: Permission denied 

window.location.href = file; 
// Opens "http://myurl.com/[object%20Blob]" 

window.location = fileURL; 
// Error: Access is denied. 

window.location = file; 
// Opens "http://myurl.com/[object%20Blob]" 

window.open= file; 
// Nothing happens 

window.open= fileURL; 
// Nothing happens 

// For all tests, using document instead of window had the exact same results 

我真的真的真的需要触发从回调的下载,考虑到:

  • 1 /生成的文件,所以没有现有的URL
  • 2 /文件生成可能需要1到20s之间的任何地方,所以任何使用超时的问题都会产生问题

无论如何,从IE中的回调开始下载?

回答

0

对于任何人发现这一点:最简单的解决方案最终使用的模块

使用FileSaver.js是:

var self = this; 

this.services.http.post("createPdf", {foo: 'bar}, {responseType: 'arraybuffer'}) 
    .then(function(response){ 
    var file = new Blob([response.data], {type: 'application/pdf'}); 
    saveAs(file, 'myDownloadedFile'); 
    }); 
+0

你见过对付IE <10什么? – godskook

0

一种解决方法是将数据缓存在服务器的内存中一小段时间(可能只是几分钟),然后将其与UUID相关联。然后从服务器返回UUID,然后让客户端发送另一个请求(这次是通过导航到一个新的URL,而不是AJAX)到服务器上的一个URL上,该URL将UUID作为参数并发回数据。

相关问题