I am currently retrieving a file from server where I set Content-Disposition: inline
in the header of the response. This will preview the file in the browser if it is supported or download the file if preview is not supported. The file is opened in a new tab which you can see from the code below.
downloadDocument(id: string) {
const documentWindow = window.open('', '_blank');
if (documentWindow) {
documentWindow.opener = null;
}
let endpoint = Endpoints.getId(Endpoints.Document, id);
this.genericService.getAll < DocumentClaim > (endpoint).subscribe(
response => {
const claim = response.claim;
endpoint = `${endpoint}/c?=${claim}`;
if (documentWindow) {
documentWindow.location.href = endpoint;
}
}
);
}
I want to be able to open the file in a new tab if preview is supported. If the browser is unable to preview the file I want to download the file in the current tab. Or at least I want to be able to close the new tab after the download has started. I have not been able to find a way to achieve this.
Is there any way to check what kind of mime types that a browser can preview? I know about window.navigator.mimeTypes
but this does not seem to give me all mime types that can be previewed. I can't find any image mimetypes in this list but images are previewed in Chrome.