Skip to content

Commit

Permalink
parse content-disposition properly and fix firefox download
Browse files Browse the repository at this point in the history
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 <unibtc@gmail.com>

use encodeUriComponent and decodeUriComponent

Signed-off-by: Uni Sayo <unibtc@gmail.com>
  • Loading branch information
uniibu authored and kittaakos committed Apr 9, 2019
1 parent 37376d0 commit ce2f145
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}]`);
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit ce2f145

Please sign in to comment.