eligrey / FileSaver.js

An HTML5 saveAs() FileSaver implementation
https://eligrey.com/blog/saving-generated-files-on-the-client-side/
Other
21.58k stars 4.38k forks source link

Add stripped down / Modern version #774

Open mhsdesign opened 2 years ago

mhsdesign commented 2 years ago

i found this lib via stackoverflow but i luckely dont need to support ancient browsers of times from the dinosaurs so i was able to strip it down to the most important parts ;) works in chrome and firefox ^^

// stripped down version of https://github.com/eligrey/FileSaver.js/blob/master/src/FileSaver.js#L81-L108

/** 
  * @param {Blob} blob
  * @param {string} name
  */
export const saveAs = (blob, name) => {
    // Namespace is used to prevent conflict w/ Chrome Poper Blocker extension (Issue https://github.com/eligrey/FileSaver.js/issues/561)
    const a = document.createElementNS('http://www.w3.org/1999/xhtml', 'a')
    a.download = name
    a.rel = 'noopener'
    a.href = URL.createObjectURL(blob)

    setTimeout(() => URL.revokeObjectURL(a.href), 40 /* sec */ * 1000)
    setTimeout(() => a.click(), 0)
}
mhsdesign commented 2 years ago

Maybe add this to the readme - so one can decide to use this 8 liner by copy pasta ^^

sojeongsojeong-park commented 1 year ago

Thank you! REALLY helpful 😘

mbrodala commented 10 months ago

Indeed, this helped us avoid the "This file cannot be downloaded securely" error which where caused by the Chrome 119.0.6045.105 update: https://chromereleases.googleblog.com/2023/10/stable-channel-update-for-desktop_31.html