FileSaver.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * FileSaver.js
  3. * A saveAs() FileSaver implementation.
  4. *
  5. * By Eli Grey, http://eligrey.com
  6. *
  7. * License : https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md (MIT)
  8. * source : http://purl.eligrey.com/github/FileSaver.js
  9. */
  10. // The one and only way of getting global scope in all environments
  11. // https://stackoverflow.com/q/3277182/1008999
  12. var _global = typeof window === 'object' && window.window === window ?
  13. window : typeof self === 'object' && self.self === self ?
  14. self : typeof global === 'object' && global.global === global ?
  15. global :
  16. this
  17. function bom(blob, opts) {
  18. if (typeof opts === 'undefined') opts = {
  19. autoBom: false
  20. }
  21. else if (typeof opts !== 'object') {
  22. console.warn('Deprecated: Expected third argument to be a object')
  23. opts = {
  24. autoBom: !opts
  25. }
  26. }
  27. // prepend BOM for UTF-8 XML and text/* types (including HTML)
  28. // note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
  29. if (opts.autoBom && /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
  30. return new Blob([String.fromCharCode(0xFEFF), blob], {
  31. type: blob.type
  32. })
  33. }
  34. return blob
  35. }
  36. function download(url, name, opts) {
  37. var xhr = new XMLHttpRequest()
  38. xhr.open('GET', url)
  39. xhr.responseType = 'blob'
  40. xhr.onload = function() {
  41. saveAs(xhr.response, name, opts)
  42. }
  43. xhr.onerror = function() {
  44. console.error('could not download file')
  45. }
  46. xhr.send()
  47. }
  48. function corsEnabled(url) {
  49. var xhr = new XMLHttpRequest()
  50. // use sync to avoid popup blocker
  51. xhr.open('HEAD', url, false)
  52. try {
  53. xhr.send()
  54. } catch (e) {}
  55. return xhr.status >= 200 && xhr.status <= 299
  56. }
  57. // `a.click()` doesn't work for all browsers (#465)
  58. function click(node) {
  59. try {
  60. node.dispatchEvent(new MouseEvent('click'))
  61. } catch (e) {
  62. var evt = document.createEvent('MouseEvents')
  63. evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80,
  64. 20, false, false, false, false, 0, null)
  65. node.dispatchEvent(evt)
  66. }
  67. }
  68. // Detect WebView inside a native macOS app by ruling out all browsers
  69. // We just need to check for 'Safari' because all other browsers (besides Firefox) include that too
  70. // https://www.whatismybrowser.com/guides/the-latest-user-agent/macos
  71. var isMacOSWebView = _global.navigator && /Macintosh/.test(navigator.userAgent) && /AppleWebKit/.test(navigator
  72. .userAgent) && !/Safari/.test(navigator.userAgent)
  73. var saveAs = _global.saveAs || (
  74. // probably in some web worker
  75. (typeof window !== 'object' || window !== _global) ?
  76. function saveAs() {
  77. /* noop */ }
  78. // Use download attribute first if possible (#193 Lumia mobile) unless this is a macOS WebView
  79. :
  80. ('download' in HTMLAnchorElement.prototype && !isMacOSWebView) ?
  81. function saveAs(blob, name, opts) {
  82. var URL = _global.URL || _global.webkitURL
  83. // Namespace is used to prevent conflict w/ Chrome Poper Blocker extension (Issue #561)
  84. var a = document.createElementNS('http://www.w3.org/1999/xhtml', 'a')
  85. name = name || blob.name || 'download'
  86. a.download = name
  87. a.rel = 'noopener' // tabnabbing
  88. // TODO: detect chrome extensions & packaged apps
  89. // a.target = '_blank'
  90. if (typeof blob === 'string') {
  91. // Support regular links
  92. a.href = blob
  93. if (a.origin !== location.origin) {
  94. corsEnabled(a.href) ?
  95. download(blob, name, opts) :
  96. click(a, a.target = '_blank')
  97. } else {
  98. click(a)
  99. }
  100. } else {
  101. // Support blobs
  102. a.href = URL.createObjectURL(blob)
  103. setTimeout(function() {
  104. URL.revokeObjectURL(a.href)
  105. }, 4E4) // 40s
  106. setTimeout(function() {
  107. click(a)
  108. }, 0)
  109. }
  110. }
  111. // Use msSaveOrOpenBlob as a second approach
  112. :
  113. 'msSaveOrOpenBlob' in navigator ?
  114. function saveAs(blob, name, opts) {
  115. name = name || blob.name || 'download'
  116. if (typeof blob === 'string') {
  117. if (corsEnabled(blob)) {
  118. download(blob, name, opts)
  119. } else {
  120. var a = document.createElement('a')
  121. a.href = blob
  122. a.target = '_blank'
  123. setTimeout(function() {
  124. click(a)
  125. })
  126. }
  127. } else {
  128. navigator.msSaveOrOpenBlob(bom(blob, opts), name)
  129. }
  130. }
  131. // Fallback to using FileReader and a popup
  132. :
  133. function saveAs(blob, name, opts, popup) {
  134. // Open a popup immediately do go around popup blocker
  135. // Mostly only available on user interaction and the fileReader is async so...
  136. popup = popup || open('', '_blank')
  137. if (popup) {
  138. popup.document.title =
  139. popup.document.body.innerText = 'downloading...'
  140. }
  141. if (typeof blob === 'string') return download(blob, name, opts)
  142. var force = blob.type === 'application/octet-stream'
  143. var isSafari = /constructor/i.test(_global.HTMLElement) || _global.safari
  144. var isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent)
  145. if ((isChromeIOS || (force && isSafari) || isMacOSWebView) && typeof FileReader !== 'undefined') {
  146. // Safari doesn't allow downloading of blob URLs
  147. var reader = new FileReader()
  148. reader.onloadend = function() {
  149. var url = reader.result
  150. url = isChromeIOS ? url : url.replace(/^data:[^;]*;/, 'data:attachment/file;')
  151. if (popup) popup.location.href = url
  152. else location = url
  153. popup = null // reverse-tabnabbing #460
  154. }
  155. reader.readAsDataURL(blob)
  156. } else {
  157. var URL = _global.URL || _global.webkitURL
  158. var url = URL.createObjectURL(blob)
  159. if (popup) popup.location = url
  160. else location.href = url
  161. popup = null // reverse-tabnabbing #460
  162. setTimeout(function() {
  163. URL.revokeObjectURL(url)
  164. }, 4E4) // 40s
  165. }
  166. }
  167. )
  168. _global.saveAs = saveAs.saveAs = saveAs
  169. if (typeof module !== 'undefined') {
  170. module.exports = saveAs;
  171. }