FileSaver.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 = { autoBom: false }
  19. else if (typeof opts !== 'object') {
  20. console.warn('Deprecated: Expected third argument to be a object')
  21. opts = { autoBom: !opts }
  22. }
  23. // prepend BOM for UTF-8 XML and text/* types (including HTML)
  24. // note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
  25. if (opts.autoBom && /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
  26. return new Blob([String.fromCharCode(0xFEFF), blob], { type: blob.type })
  27. }
  28. return blob
  29. }
  30. function download (url, name, opts) {
  31. var xhr = new XMLHttpRequest()
  32. xhr.open('GET', url)
  33. xhr.responseType = 'blob'
  34. xhr.onload = function () {
  35. saveAs(xhr.response, name, opts)
  36. }
  37. xhr.onerror = function () {
  38. console.error('could not download file')
  39. }
  40. xhr.send()
  41. }
  42. function corsEnabled (url) {
  43. var xhr = new XMLHttpRequest()
  44. // use sync to avoid popup blocker
  45. xhr.open('HEAD', url, false)
  46. try {
  47. xhr.send()
  48. } catch (e) {}
  49. return xhr.status >= 200 && xhr.status <= 299
  50. }
  51. // `a.click()` doesn't work for all browsers (#465)
  52. function click (node) {
  53. try {
  54. node.dispatchEvent(new MouseEvent('click'))
  55. } catch (e) {
  56. var evt = document.createEvent('MouseEvents')
  57. evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80,
  58. 20, false, false, false, false, 0, null)
  59. node.dispatchEvent(evt)
  60. }
  61. }
  62. var saveAs = _global.saveAs || (
  63. // probably in some web worker
  64. (typeof window !== 'object' || window !== _global)
  65. ? function saveAs () { /* noop */ }
  66. // Use download attribute first if possible (#193 Lumia mobile)
  67. : 'download' in HTMLAnchorElement.prototype
  68. ? function saveAs (blob, name, opts) {
  69. var URL = _global.URL || _global.webkitURL
  70. var a = document.createElement('a')
  71. name = name || blob.name || 'download'
  72. a.download = name
  73. a.rel = 'noopener' // tabnabbing
  74. // TODO: detect chrome extensions & packaged apps
  75. // a.target = '_blank'
  76. if (typeof blob === 'string') {
  77. // Support regular links
  78. a.href = blob
  79. if (a.origin !== location.origin) {
  80. corsEnabled(a.href)
  81. ? download(blob, name, opts)
  82. : click(a, a.target = '_blank')
  83. } else {
  84. click(a)
  85. }
  86. } else {
  87. // Support blobs
  88. a.href = URL.createObjectURL(blob)
  89. setTimeout(function () { URL.revokeObjectURL(a.href) }, 4E4) // 40s
  90. setTimeout(function () { click(a) }, 0)
  91. }
  92. }
  93. // Use msSaveOrOpenBlob as a second approach
  94. : 'msSaveOrOpenBlob' in navigator
  95. ? function saveAs (blob, name, opts) {
  96. name = name || blob.name || 'download'
  97. if (typeof blob === 'string') {
  98. if (corsEnabled(blob)) {
  99. download(blob, name, opts)
  100. } else {
  101. var a = document.createElement('a')
  102. a.href = blob
  103. a.target = '_blank'
  104. setTimeout(function () { click(a) })
  105. }
  106. } else {
  107. navigator.msSaveOrOpenBlob(bom(blob, opts), name)
  108. }
  109. }
  110. // Fallback to using FileReader and a popup
  111. : function saveAs (blob, name, opts, popup) {
  112. // Open a popup immediately do go around popup blocker
  113. // Mostly only available on user interaction and the fileReader is async so...
  114. popup = popup || open('', '_blank')
  115. if (popup) {
  116. popup.document.title =
  117. popup.document.body.innerText = 'downloading...'
  118. }
  119. if (typeof blob === 'string') return download(blob, name, opts)
  120. var force = blob.type === 'application/octet-stream'
  121. var isSafari = /constructor/i.test(_global.HTMLElement) || _global.safari
  122. var isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent)
  123. if ((isChromeIOS || (force && isSafari)) && typeof FileReader !== 'undefined') {
  124. // Safari doesn't allow downloading of blob URLs
  125. var reader = new FileReader()
  126. reader.onloadend = function () {
  127. var url = reader.result
  128. url = isChromeIOS ? url : url.replace(/^data:[^;]*;/, 'data:attachment/file;')
  129. if (popup) popup.location.href = url
  130. else location = url
  131. popup = null // reverse-tabnabbing #460
  132. }
  133. reader.readAsDataURL(blob)
  134. } else {
  135. var URL = _global.URL || _global.webkitURL
  136. var url = URL.createObjectURL(blob)
  137. if (popup) popup.location = url
  138. else location.href = url
  139. popup = null // reverse-tabnabbing #460
  140. setTimeout(function () { URL.revokeObjectURL(url) }, 4E4) // 40s
  141. }
  142. }
  143. )
  144. _global.saveAs = saveAs.saveAs = saveAs
  145. if (typeof module !== 'undefined') {
  146. module.exports = saveAs;
  147. }