From 8c17929151f9c4fa5f48564492bbf400ced95eea Mon Sep 17 00:00:00 2001 From: undergroundwires Date: Thu, 3 Sep 2020 00:48:22 +0100 Subject: [PATCH] fix some browsers (including firefox) downloading the script as a text file --- src/infrastructure/SaveFileDialog.ts | 13 +++++++++++-- src/presentation/TheCodeButtons.vue | 4 ++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/infrastructure/SaveFileDialog.ts b/src/infrastructure/SaveFileDialog.ts index c8ec0027..b5fc02ac 100644 --- a/src/infrastructure/SaveFileDialog.ts +++ b/src/infrastructure/SaveFileDialog.ts @@ -1,9 +1,18 @@ import fileSaver from 'file-saver'; +export enum FileType { + BatchFile, +} export class SaveFileDialog { - public static saveText(text: string, fileName: string): void { - this.saveBlob(text, 'text/plain;charset=utf-8', fileName); + public static saveFile(text: string, fileName: string, type: FileType): void { + const mimeType = this.mimeTypes.get(type); + this.saveBlob(text, mimeType, fileName); } + private static readonly mimeTypes = new Map([ + // Some browsers (including firefox + IE) require right mime type + // otherwise they ignore extension and save the file as text. + [ FileType.BatchFile, 'application/bat' ], // https://en.wikipedia.org/wiki/Batch_file + ]); private static saveBlob(file: BlobPart, fileType: string, fileName: string): void { try { diff --git a/src/presentation/TheCodeButtons.vue b/src/presentation/TheCodeButtons.vue index 9a0b830d..5bd08617 100644 --- a/src/presentation/TheCodeButtons.vue +++ b/src/presentation/TheCodeButtons.vue @@ -16,7 +16,7 @@