繁体   English   中英

如何强制在iPhone的新标签页中打开文件下载?

[英]How can I force a file download to open in a new tab on iPhone?

在我的Angular应用中,我试图在移动设备上单击时在新标签中打开图像,pdf和其他各种下载链接。 这在台式机上可以正常工作,但是移动设备会在当前窗口中继续打开下载,并强迫用户使用浏览器的后退按钮。

  <span class="link" (click)="viewFile(document.docID)">{{document.comments}}/span>


  viewFile(documentName: string){
    this.spinner.show();
    this.formService.viewFile(documentName).subscribe((data: any) => {
      this.spinner.hide();
      const link = document.createElement('a');
      link.href = window.URL.createObjectURL(data);
      link.target = '_blank';
      link.click();
    }, (error) => {
      this.spinner.hide();
      this.toastr.error(error);
    });
  }

I have also tried window.open(link.href, '_blank') with no success

如何修改我的viewFile函数,以强制下载在移动设备的新标签页中打开?

我正在通过https服务我的应用程序和下载内容。 混合内容不是问题。

为什么不简单使用锚来明确指示浏览器打开新页面呢?

<span> 
   <a [href]="getFileLink(document.docID)" target="_blank">
      {{document.comments}}
   </a>
</span>

事实证明,这是由于本机弹出窗口阻止程序触发而导致的,因为用户操作离新窗口打开的位置太远了。 为了解决这个问题,我在点击时创建了window对象,然后在回调返回后重新分配它。

 viewFile(docID: string, isLocalDoc) {
    const newWindow = window.open(this.formService.setLoadingUrl());
    // timeout put in place b/c it returns too quickly for the new window without it (results in the window closing early)
    setTimeout(() => {
      this.spinner.show();
      this.formService.viewFile(docID, isLocalDoc).subscribe((data: any) => {
        this.spinner.hide();
        setTimeout(() => {
          newWindow.location.href = window.URL.createObjectURL(data);
          if (data.type === 'application/octet-stream') {
            newWindow.close();
          }
        }, 500);
      }, (error) => {
        this.spinner.hide();
        newWindow.close();
        this.toastr.error(error);
      });
    }, 500);
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM