not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
5.2 KiB

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