Skip to content
Snippets Groups Projects
Select Git revision
  • 8e1876fc25c219d26b6c9e85a543e7ade324d434
  • master default protected
  • rednatco-v2
  • rednatco
  • test
  • ntc-tube-uniform-color
  • ntc-tube-missing-atoms
  • restore-vertex-array-per-program
  • watlas2
  • dnatco_new
  • cleanup-old-nodejs
  • webmmb
  • fix_auth_seq_id
  • update_deps
  • ext_dev
  • ntc_balls
  • nci-2
  • plugin
  • bugfix-0.4.5
  • nci
  • servers
  • v0.5.0-dev.1
  • v0.4.5
  • v0.4.4
  • v0.4.3
  • v0.4.2
  • v0.4.1
  • v0.4.0
  • v0.3.12
  • v0.3.11
  • v0.3.10
  • v0.3.9
  • v0.3.8
  • v0.3.7
  • v0.3.6
  • v0.3.5
  • v0.3.4
  • v0.3.3
  • v0.3.2
  • v0.3.1
  • v0.3.0
41 results

file-info.ts

Blame
  • download.ts 1.90 KiB
    /**
     * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
     *
     * @author Alexander Rose <alexander.rose@weirdbyte.de>
     */
    
    function openUrl (url: string) {
        const opened = window.open(url, '_blank')
        if (!opened) {
            window.location.href = url
        }
    }
    
    export function download (data: Blob|string, downloadName = 'download') {
        // using ideas from https://github.com/eligrey/FileSaver.js/blob/master/FileSaver.js
    
        if (!data) return
    
        const ua = window.navigator.userAgent
        const isSafari = /Safari/i.test(ua)
        const isChromeIos = /CriOS\/[\d]+/.test(ua)
    
        const a = document.createElement('a')
    
        function open (str: string) {
            openUrl(isChromeIos ? str : str.replace(/^data:[^;]*;/, 'data:attachment/file;'))
        }
    
        if (typeof navigator !== 'undefined' && navigator.msSaveOrOpenBlob) {
            // native saveAs in IE 10+
            navigator.msSaveOrOpenBlob(data, downloadName)
        } else if ((isSafari || isChromeIos) && FileReader) {
            if (data instanceof Blob) {
                // no downloading of blob urls in Safari
                const reader = new FileReader()
                reader.onloadend = () => open(reader.result as string)
                reader.readAsDataURL(data)
            } else {
                open(data)
            }
        } else {
            let objectUrlCreated = false
            if (data instanceof Blob) {
                data = URL.createObjectURL(data)
                objectUrlCreated = true
            }
    
            if ('download' in a) {
                // download link available
                a.style.display = 'hidden'
                document.body.appendChild(a)
                a.href = data
                a.download = downloadName
                a.target = '_blank'
                a.click()
                document.body.removeChild(a)
            } else {
                openUrl(data)
            }
    
            if (objectUrlCreated) {
                window.URL.revokeObjectURL(data)
            }
        }
    }