Skip to content

Commit 531a517

Browse files
committed
Fix download for Edge browser
1 parent 1b76bef commit 531a517

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

client/src/app/download-function-app-content/download-function-app-content.component.ts

+10-13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { Subject } from 'rxjs/Subject';
66
import { Component, Input, Output } from '@angular/core';
77
import { KeyCodes } from 'app/shared/models/constants';
88
import { FunctionAppService } from '../shared/services/function-app.service';
9+
import { FileUtilities } from '../shared/Utilities/file';
10+
import { GlobalStateService } from '../shared/services/global-state.service';
911

1012
type DownloadOption = 'siteContent' | 'vsProject';
1113

@@ -22,7 +24,9 @@ export class DownloadFunctionAppContentComponent {
2224
public currentDownloadOption: DownloadOption;
2325
public includeAppSettings: boolean;
2426

25-
constructor(private _translateService: TranslateService, private _functionAppService: FunctionAppService) {
27+
constructor(private _translateService: TranslateService,
28+
private _functionAppService: FunctionAppService,
29+
private _globalStateService: GlobalStateService) {
2630
this.close = new Subject<boolean>();
2731
this.downloadOptions = [{
2832
displayLabel: this._translateService.instant(PortalResources.downloadFunctionAppContent_siteContent),
@@ -38,22 +42,15 @@ export class DownloadFunctionAppContentComponent {
3842
downloadFunctionAppContent() {
3943
if (this.context) {
4044
const includeCsProj = this.currentDownloadOption === 'siteContent' ? false : true;
41-
42-
// https://github.com/eligrey/FileSaver.js/blob/00e540fda507173f83a9408f1604622538d0c81a/src/FileSaver.js#L128-L136
43-
const anchor = document.createElement('a');
44-
anchor.style.display = 'none';
45-
document.body.appendChild(anchor);
45+
this._globalStateService.setBusyState();
46+
this.closeModal();
4647
this._functionAppService.getAppContentAsZip(this.context, includeCsProj, this.includeAppSettings)
4748
.subscribe(data => {
4849
if (data.isSuccessful) {
49-
const url = window.URL.createObjectURL(data.result);
50-
anchor.href = url;
51-
anchor.download = `${this.context.site.name}.zip`;
52-
anchor.click();
53-
window.URL.revokeObjectURL(url);
50+
FileUtilities.saveFile(data.result, `${this.context.site.name}.zip`);
5451
}
55-
anchor.remove();
56-
});
52+
this._globalStateService.clearBusyState();
53+
}, () => this._globalStateService.clearBusyState());
5754
}
5855
}
5956

client/src/app/shared/Utilities/file.ts

+22
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,26 @@ export class FileUtilities {
77
public static isBinary(fileName: string): boolean {
88
return !!fileName && FileUtilities.binaryExtensions.some(e => fileName.toLowerCase().endsWith(e));
99
}
10+
11+
// https://github.com/eligrey/FileSaver.js/blob/00e540fda507173f83a9408f1604622538d0c81a/src/FileSaver.js#L128-L136
12+
public static saveFile(blob: Blob, fileName: string) {
13+
const windowUrl = window.URL || (<any>window).webkitURL;
14+
if (window.navigator.msSaveOrOpenBlob) {
15+
// Currently, Edge doesn' respect the "download" attribute to name the file from blob
16+
// https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/7260192/
17+
window.navigator.msSaveOrOpenBlob(blob, fileName);
18+
} else {
19+
const anchor = document.createElement('a');
20+
anchor.style.display = 'none';
21+
document.body.appendChild(anchor);
22+
// http://stackoverflow.com/questions/37432609/how-to-avoid-adding-prefix-unsafe-to-link-by-angular2
23+
setTimeout(() => {
24+
const url = windowUrl.createObjectURL(blob);
25+
anchor.href = url;
26+
anchor.download = fileName;
27+
anchor.click();
28+
window.URL.revokeObjectURL(url);
29+
});
30+
}
31+
}
1032
}

0 commit comments

Comments
 (0)