fix some browsers (including firefox) downloading the script as a text file
This commit is contained in:
@@ -1,9 +1,18 @@
|
|||||||
import fileSaver from 'file-saver';
|
import fileSaver from 'file-saver';
|
||||||
|
|
||||||
|
export enum FileType {
|
||||||
|
BatchFile,
|
||||||
|
}
|
||||||
export class SaveFileDialog {
|
export class SaveFileDialog {
|
||||||
public static saveText(text: string, fileName: string): void {
|
public static saveFile(text: string, fileName: string, type: FileType): void {
|
||||||
this.saveBlob(text, 'text/plain;charset=utf-8', fileName);
|
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 {
|
private static saveBlob(file: BlobPart, fileType: string, fileName: string): void {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { Component } from 'vue-property-decorator';
|
import { Component } from 'vue-property-decorator';
|
||||||
import { StatefulVue } from './StatefulVue';
|
import { StatefulVue } from './StatefulVue';
|
||||||
import { SaveFileDialog } from './../infrastructure/SaveFileDialog';
|
import { SaveFileDialog, FileType } from './../infrastructure/SaveFileDialog';
|
||||||
import { Clipboard } from './../infrastructure/Clipboard';
|
import { Clipboard } from './../infrastructure/Clipboard';
|
||||||
import IconButton from './IconButton.vue';
|
import IconButton from './IconButton.vue';
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@ export default class TheCodeButtons extends StatefulVue {
|
|||||||
|
|
||||||
public async saveCodeAsync() {
|
public async saveCodeAsync() {
|
||||||
const state = await this.getCurrentStateAsync();
|
const state = await this.getCurrentStateAsync();
|
||||||
SaveFileDialog.saveText(state.code.current, 'privacy-script.bat');
|
SaveFileDialog.saveFile(state.code.current, 'privacy-script.bat', FileType.BatchFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user