From ce2f145017ff6bca715cddb96f876366eb1a4b22 Mon Sep 17 00:00:00 2001 From: Uni Sayo Date: Fri, 29 Mar 2019 15:06:01 +0000 Subject: [PATCH] parse content-disposition properly and fix firefox download This commit Fixes #4754 and #4248. It uses express' own attachement method and parses the content-disposition reliably and decodes the filename if possible. This also fixes the downloading on firefox by appending the anchor link to the body before triggering the click event and removes it after. Signed-off-by: Uni Sayo use encodeUriComponent and decodeUriComponent Signed-off-by: Uni Sayo --- .../src/browser/download/file-download-service.ts | 9 +++++++-- .../src/node/download/file-download-handler.ts | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/filesystem/src/browser/download/file-download-service.ts b/packages/filesystem/src/browser/download/file-download-service.ts index b870a97ca5824..9f057c905ca38 100644 --- a/packages/filesystem/src/browser/download/file-download-service.ts +++ b/packages/filesystem/src/browser/download/file-download-service.ts @@ -168,7 +168,7 @@ export class FileDownloadService { const title = await this.title(response, uris); const { status, statusText } = response; if (status === 200) { - await this.forceDownload(response, title); + await this.forceDownload(response, decodeURIComponent(title)); } else { throw new Error(`Received unexpected status code: ${status}. [${statusText}]`); } @@ -194,12 +194,17 @@ export class FileDownloadService { url = URL.createObjectURL(blob); if (this.anchor === undefined) { this.anchor = document.createElement('a'); - this.anchor.style.display = 'none'; } this.anchor.href = url; + this.anchor.style.display = 'none'; this.anchor.download = title; + document.body.appendChild(this.anchor); this.anchor.click(); } finally { + // make sure anchor is removed from parent + if (this.anchor && this.anchor.parentNode) { + this.anchor.parentNode.removeChild(this.anchor); + } if (url) { URL.revokeObjectURL(url); } diff --git a/packages/filesystem/src/node/download/file-download-handler.ts b/packages/filesystem/src/node/download/file-download-handler.ts index 52888e49fe182..f812b6df71d66 100644 --- a/packages/filesystem/src/node/download/file-download-handler.ts +++ b/packages/filesystem/src/node/download/file-download-handler.ts @@ -53,7 +53,7 @@ export abstract class FileDownloadHandler { } else { this.logger.debug(`Cannot determine the content-type for file: ${filePath}. Skipping the 'Content-type' header from the HTTP response.`); } - response.setHeader('Content-Disposition', `attachment; filename=${name}`); + response.setHeader('Content-Disposition', `attachment; filename=${encodeURIComponent(name)}`); try { await fs.access(filePath, fs.constants.R_OK); fs.readFile(filePath, (error, data) => {