Skip to content

Commit

Permalink
fix some browsers (including firefox) downloading the script as a tex…
Browse files Browse the repository at this point in the history
…t file
  • Loading branch information
undergroundwires committed Sep 4, 2020
1 parent bb92c9e commit 8c17929
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/infrastructure/SaveFileDialog.ts
Original file line number Diff line number Diff line change
@@ -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<FileType, string>([
// 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 {
Expand Down
4 changes: 2 additions & 2 deletions src/presentation/TheCodeButtons.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<script lang="ts">
import { Component } from 'vue-property-decorator';
import { StatefulVue } from './StatefulVue';
import { SaveFileDialog } from './../infrastructure/SaveFileDialog';
import { SaveFileDialog, FileType } from './../infrastructure/SaveFileDialog';
import { Clipboard } from './../infrastructure/Clipboard';
import IconButton from './IconButton.vue';
Expand All @@ -43,7 +43,7 @@ export default class TheCodeButtons extends StatefulVue {
public async saveCodeAsync() {
const state = await this.getCurrentStateAsync();
SaveFileDialog.saveText(state.code.current, 'privacy-script.bat');
SaveFileDialog.saveFile(state.code.current, 'privacy-script.bat', FileType.BatchFile);
}
}
</script>
Expand Down

0 comments on commit 8c17929

Please sign in to comment.