FileSaver.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* FileSaver.js
  2. * A saveAs() FileSaver implementation.
  3. * 1.3.2
  4. * 2016-06-16 18:25:19
  5. *
  6. * By Eli Grey, http://eligrey.com
  7. * License: MIT
  8. * See https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md
  9. */
  10. /* global self */
  11. /* jslint bitwise: true, indent: 4, laxbreak: true, laxcomma: true, smarttabs: true, plusplus: true */
  12. /*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */
  13. var saveAs = saveAs || (function (view) {
  14. 'use strict'
  15. // IE <10 is explicitly unsupported
  16. if (typeof view === 'undefined' || typeof navigator !== 'undefined' && /MSIE [1-9]\./.test(navigator.userAgent)) {
  17. return
  18. }
  19. var
  20. doc = view.document
  21. // only get URL when necessary in case Blob.js hasn't overridden it yet
  22. var get_URL = function () {
  23. return view.URL || view.webkitURL || view
  24. }
  25. var save_link = doc.createElementNS('http://www.w3.org/1999/xhtml', 'a')
  26. var can_use_save_link = 'download' in save_link
  27. var click = function (node) {
  28. var event = new MouseEvent('click')
  29. node.dispatchEvent(event)
  30. }
  31. var is_safari = /constructor/i.test(view.HTMLElement) || view.safari
  32. var is_chrome_ios = /CriOS\/[\d]+/.test(navigator.userAgent)
  33. var throw_outside = function (ex) {
  34. (view.setImmediate || view.setTimeout)(function () {
  35. throw ex
  36. }, 0)
  37. }
  38. var force_saveable_type = 'application/octet-stream'
  39. // the Blob API is fundamentally broken as there is no "downloadfinished" event to subscribe to
  40. var arbitrary_revoke_timeout = 1000 * 40 // in ms
  41. var revoke = function (file) {
  42. var revoker = function () {
  43. if (typeof file === 'string') { // file is an object URL
  44. get_URL().revokeObjectURL(file)
  45. } else { // file is a File
  46. file.remove()
  47. }
  48. }
  49. setTimeout(revoker, arbitrary_revoke_timeout)
  50. }
  51. var dispatch = function (filesaver, event_types, event) {
  52. event_types = [].concat(event_types)
  53. var i = event_types.length
  54. while (i--) {
  55. var listener = filesaver['on' + event_types[i]]
  56. if (typeof listener === 'function') {
  57. try {
  58. listener.call(filesaver, event || filesaver)
  59. } catch (ex) {
  60. throw_outside(ex)
  61. }
  62. }
  63. }
  64. }
  65. var auto_bom = function (blob) {
  66. // prepend BOM for UTF-8 XML and text/* types (including HTML)
  67. // note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
  68. if (/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
  69. return new Blob([String.fromCharCode(0xFEFF), blob], {type: blob.type})
  70. }
  71. return blob
  72. }
  73. var FileSaver = function (blob, name, no_auto_bom) {
  74. if (!no_auto_bom) {
  75. blob = auto_bom(blob)
  76. }
  77. // First try a.download, then web filesystem, then object URLs
  78. var
  79. filesaver = this
  80. var type = blob.type
  81. var force = type === force_saveable_type
  82. var object_url
  83. var dispatch_all = function () {
  84. dispatch(filesaver, 'writestart progress write writeend'.split(' '))
  85. }
  86. // on any filesys errors revert to saving with object URLs
  87. var fs_error = function () {
  88. if ((is_chrome_ios || (force && is_safari)) && view.FileReader) {
  89. // Safari doesn't allow downloading of blob urls
  90. var reader = new FileReader()
  91. reader.onloadend = function () {
  92. var url = is_chrome_ios ? reader.result : reader.result.replace(/^data:[^;]*;/, 'data:attachment/file;')
  93. var popup = view.open(url, '_blank')
  94. if (!popup) view.location.href = url
  95. url = undefined // release reference before dispatching
  96. filesaver.readyState = filesaver.DONE
  97. dispatch_all()
  98. }
  99. reader.readAsDataURL(blob)
  100. filesaver.readyState = filesaver.INIT
  101. return
  102. }
  103. // don't create more object URLs than needed
  104. if (!object_url) {
  105. object_url = get_URL().createObjectURL(blob)
  106. }
  107. if (force) {
  108. view.location.href = object_url
  109. } else {
  110. var opened = view.open(object_url, '_blank')
  111. if (!opened) {
  112. // Apple does not allow window.open, see https://developer.apple.com/library/safari/documentation/Tools/Conceptual/SafariExtensionGuide/WorkingwithWindowsandTabs/WorkingwithWindowsandTabs.html
  113. view.location.href = object_url
  114. }
  115. }
  116. filesaver.readyState = filesaver.DONE
  117. dispatch_all()
  118. revoke(object_url)
  119. }
  120. filesaver.readyState = filesaver.INIT
  121. if (can_use_save_link) {
  122. object_url = get_URL().createObjectURL(blob)
  123. setTimeout(function () {
  124. save_link.href = object_url
  125. save_link.download = name
  126. click(save_link)
  127. dispatch_all()
  128. revoke(object_url)
  129. filesaver.readyState = filesaver.DONE
  130. })
  131. return
  132. }
  133. fs_error()
  134. }
  135. var FS_proto = FileSaver.prototype
  136. var saveAs = function (blob, name, no_auto_bom) {
  137. return new FileSaver(blob, name || blob.name || 'download', no_auto_bom)
  138. }
  139. // IE 10+ (native saveAs)
  140. if (typeof navigator !== 'undefined' && navigator.msSaveOrOpenBlob) {
  141. return function (blob, name, no_auto_bom) {
  142. name = name || blob.name || 'download'
  143. if (!no_auto_bom) {
  144. blob = auto_bom(blob)
  145. }
  146. return navigator.msSaveOrOpenBlob(blob, name)
  147. }
  148. }
  149. FS_proto.abort = function () {
  150. }
  151. FS_proto.readyState = FS_proto.INIT = 0
  152. FS_proto.WRITING = 1
  153. FS_proto.DONE = 2
  154. FS_proto.error =
  155. FS_proto.onwritestart =
  156. FS_proto.onprogress =
  157. FS_proto.onwrite =
  158. FS_proto.onabort =
  159. FS_proto.onerror =
  160. FS_proto.onwriteend =
  161. null
  162. return saveAs
  163. }(
  164. typeof self !== 'undefined' && self ||
  165. typeof window !== 'undefined' && window ||
  166. this.content
  167. ))
  168. // `self` is undefined in Firefox for Android content script context
  169. // while `this` is nsIContentFrameMessageManager
  170. // with an attribute `content` that corresponds to the window
  171. if (typeof module !== 'undefined' && module.exports) {
  172. module.exports.saveAs = saveAs
  173. } else if ((typeof define !== 'undefined' && define !== null) && (define.amd !== null)) {
  174. define('FileSaver.js', function () {
  175. return saveAs
  176. })
  177. }