After Chrome version 83 release, file download in modal dialog doesn't work

寵の児 提交于 2020-06-09 04:40:47

问题


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

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.

Script is as follows.

/**
 * 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');
}


回答1:


Problem

Cannot download a file via link in modal dialog

Reason

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:

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.

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

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

What to do

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.



来源:https://stackoverflow.com/questions/62127708/after-chrome-version-83-release-file-download-in-modal-dialog-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!