1. 程式人生 > >利用Javascript生成txt文本文件

利用Javascript生成txt文本文件

nec clas utf-8 blog immediate exp alt eve ongui

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

http://blog.csdn.net/baidu_34036884/article/details/68070550

利用Javascript生成txt文本文件