首页 > 解决方案 > 当我使用 window.open() 时,我无法在新窗口中打开文件

问题描述

我正在使用此代码打开文件,它会在控制台中显示 url,但不会在新窗口中打开它。

ft.openFile = function(id) {
  FileService.download(id).then(function(resp) {
    console.log(resp.headers('Content-Type'));
    var blob = new Blob([resp.data], {
      type: resp.headers('Content-Type')
    });
    var url = $window.URL || $window.webkitURL;
    var fileUrl = url.createObjectURL(blob);
    window.open(fileUrl);
    console.log(fileUrl);
})

标签: node.jsangularjsmongodbexpress

解决方案


 ft.openFile = function(id){
 FileService.download(id).then(function(resp){
    console.log(resp.headers('Content-Type'));
    var blob = new Blob([resp.data],{type: resp.headers('Content-Type')});
    var url = $window.URL||$window.webkitURL;
    var fileUrl = url.createObjectURL(blob);

    window.open(fileUrl, '_self'); //same window

    //or

    window.open(fileUrl, '_blank'); //new window

    console.log(fileUrl);
 })

推荐阅读