利用Javascript生成txt文本文件

别说谁变了你拦得住时间么 提交于 2020-03-03 00:38:35
  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.   
  113.             if (can_use_save_link) {  
  114.                 object_url = get_URL().createObjectURL(blob);  
  115.                 setTimeout(function() {  
  116.                     save_link.href = object_url;  
  117.                     save_link.download = name;  
  118.                     click(save_link);  
  119.                     dispatch_all();  
  120.                     revoke(object_url);  
  121.                     filesaver.readyState = filesaver.DONE;  
  122.                 });  
  123.                 return;  
  124.             }  
  125.   
  126.             fs_error();  
  127.         }  
  128.         , FS_proto = FileSaver.prototype  
  129.         , saveAs = function(blob, name, no_auto_bom) {  
  130.             return new FileSaver(blob, name || blob.name || "download", no_auto_bom);  
  131.         }  
  132.     ;  
  133.     // IE 10+ (native saveAs)  
  134.     if (typeof navigator !== "undefined" && navigator.msSaveOrOpenBlob) {  
  135.         return function(blob, name, no_auto_bom) {  
  136.             name = name || blob.name || "download";  
  137.   
  138.             if (!no_auto_bom) {  
  139.                 blob = auto_bom(blob);  
  140.             }  
  141.             return navigator.msSaveOrOpenBlob(blob, name);  
  142.         };  
  143.     }  
  144.   
  145.     FS_proto.abort = function(){};  
  146.     FS_proto.readyState = FS_proto.INIT = 0;  
  147.     FS_proto.WRITING = 1;  
  148.     FS_proto.DONE = 2;  
  149.   
  150.     FS_proto.error =  
  151.     FS_proto.onwritestart =  
  152.     FS_proto.onprogress =  
  153.     FS_proto.onwrite =  
  154.     FS_proto.onabort =  
  155.     FS_proto.onerror =  
  156.     FS_proto.onwriteend =  
  157.         null;  
  158.   
  159.     return saveAs;  
  160. }(  
  161.        typeof self !== "undefined" && self  
  162.     || typeof window !== "undefined" && window  
  163.     || this.content  
  164. ));  
  165. // `self` is undefined in Firefox for Android content script context  
  166. // while `this` is nsIContentFrameMessageManager  
  167. // with an attribute `content` that corresponds to the window  
  168.   
  169. if (typeof module !== "undefined" && module.exports) {  
  170.   module.exports.saveAs = saveAs;  
  171. else if ((typeof define !== "undefined" && define !== null) && (define.amd !== null)) {  
  172.   define("FileSaver.js", function() {  
  173.     return saveAs;  
  174.   });  
  175. }  
  176.   
  177. // 引入上边的js后,就可以调用生成文本的方法 另外ie下会有中文乱码的问题  
  178. var blob = new Blob(["Hello, world!\t\n我是milo,你好啊"], {type: "text/plain;charset=utf-8"});  
  179. saveAs(blob, "hello world.txt");  
  180. </script>  

 

 

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

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!