Chrome版本83发行后,模式对话框中的文件下载不起作用 [英] After Chrome version 83 release, file download in modal dialog doesn't work

查看:628
本文介绍了Chrome版本83发行后,模式对话框中的文件下载不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Google Apps脚本快速下载电子表格数据。

I use Google Apps Script to make download Spreadsheet data quickly.

几天前,下载功能突然失效。
我的一些可以使用此功能的同事使用的是chrome版本81.0.4044.138(官方内部版本)
而不能使用该功能的chrome版本的83.0.4103.61(官方内部版本)

Since several days before, download function doesn't work suddenly. Some of my coworkers who can use the function use chrome version 81.0.4044.138(Official Build) and who cannot use the function , chrome version 83.0.4103.61(Official Build)

(右键单击并[另存为]很幸运)

(right-click and [save as] works fortunately)

我想知道应该怎么做才能实现一键下载功能再次处于活动状态。

I want to know what should I do to make one-click download function active again.

脚本如下。

/**
 * Adds a custom menu
 *
 * @param {Object} e The event parameter for a simple onOpen trigger.
 */
function onOpen(e) {
  SpreadsheetApp.getUi()
      .createMenu('Custom')
      .addItem('Download as XLSX', 'downloadXLS_GUI')
      .addToUi();
}


/**
 * Display a modal dialog with a single download link.
 *
 * From: http://stackoverflow.com/a/37336778/1677912
 */
function downloadXLS_GUI() {
  // Get current spreadsheet's ID, place in download URL
  var ssID = SpreadsheetApp.getActive().getId();
  var URL = 'https://docs.google.com/spreadsheets/d/'+ssID+'/export?format=xlsx';

  // Display a modal dialog box with download link.
  var htmlOutput = HtmlService
                  .createHtmlOutput('<a href="'+URL+'">Click to download</a>')
                  .setSandboxMode(HtmlService.SandboxMode.IFRAME)
                  .setWidth(800)
                  .setHeight(600);
  SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Download XLS');
}


推荐答案

问题

无法通过模式对话框中的链接下载文件

Cannot download a file via link in modal dialog

原因

这样做的能力是自Chrome 83起已弃用(因此出现了问题)。模态对话框是沙盒环境,因此脚本停止工作。如果打开开发人员工具(按 f12 ),则会看到警告:

The ability to do so is deprecated since Chrome 83 (hence the issue). Modal dialogs are sandbox environments, therefore your script stopped working. If you open the developer tools (press f12), you will see a warning:


不允许下载。启动或实例化下载的框架被沙盒化,但是未设置允许下载标志。有关更多详细信息,请参见 https://www.chromestatus.com/feature/5706745674465280 。 p>

Download is disallowed. The frame initiating or instantiating the download is sandboxed, but the flag ‘allow-downloads’ is not set. See https://www.chromestatus.com/feature/5706745674465280 for more details.

在链接中添加 download 属性可以缓解该问题,但是您会遇到第二次警告:

Adding a download attribute to the link mitigates that part, but you will encounter the second warning:


资源被解释为Document,但以MIME类型application / vnd.openxmlformats-officedocument.spreadsheetml.sheet

Resource interpreted as Document but transferred with MIME type application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

要做的事

到目前为止, 此问题尚未解决,并承诺允许-下载标志将添加到 HtmlService.XFrameOptionsMode.ALLOWALL -您可以通过 setXFrameOptionsMode将选项添加到模式对话框中并查找更新。

As of now, there is an open issue regarding the matter, and it is promised that the allow-downloads flag will be added to HtmlService.XFrameOptionsMode.ALLOWALL - you can add the option to modal dialog via setXFrameOptionsMode and look for the updates.

这篇关于Chrome版本83发行后,模式对话框中的文件下载不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆