js 点击默认另存 ,不是打开 Blob 操作
function savepic(obj) { if (memFileObj != undefined) { obj = memFileObj; } else { memFileObj = obj; } if (document.all.a1 == null) { objIframe = document.createElement("IFRAME"); $(objIframe).appendTo("body"); objIframe.outerHTML = "<iframe name='a1' style='width:0;hieght:0' src='" + $(obj).attr("href") + "'></iframe>"; re = setTimeout("savepic()", 1); } else { clearTimeout(re); pic = window.open($(obj).attr("href"), "a1"); pic.document.execCommand("SaveAs"); var _parentElement = $("[name='a1']"); if (_parentElement) { $(_parentElement).remove(); //_parentElement.removeChild(a1); } }
其他浏览器,例如Chrome浏览器,可以使用保存blob数据流的办法。
给你两个函数供参考
一个用来保存URL,一个用来保存文本。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
function saveAs(Url,filename){ var blob= new Blob([ '' ], {type: 'application/octet-stream' }); var url = webkitURL.createObjectURL(blob); var a = document.createElementNS(xhtml, 'a' ); a.href = Url; a.download = filename; var e = document.createEvent( 'MouseEvents' ); e.initMouseEvent( 'click' , true , false , window, 0, 0, 0, 0, 0, false , false , false , false , 0, null ); a.dispatchEvent(e); webkitURL.revokeObjectURL(url); } function saveText(t,filename){ var mime= 'text/plain' ; saveAs(( 'data:' +mime+ ';charset=utf-8;base64,' + Base64.encode(t)),filename) } |
function savepic(obj) { if (memFileObj != undefined) { obj = memFileObj; } else { memFileObj = obj; } if (document.all.a1 == null) { objIframe = document.createElement("IFRAME"); $(objIframe).appendTo("body"); objIframe.outerHTML = "<iframe name='a1' style='width:0;hieght:0' src='" + $(obj).attr("href") + "'></iframe>"; re = setTimeout("savepic()", 1); } else { var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串 var isOpera = userAgent.indexOf("Opera") > -1; clearTimeout(re); pic = window.open($(obj).attr("href"), "a1"); pic.document.execCommand("SaveAs"); var frameElement = $("[name='a1']"); if (frameElement) { $(frameElement).remove(); } // if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) { // is ie // } else { //not ie // saveAs($(obj).attr("href"), $(obj).attr("href").substring($(obj).attr("href").lastIndexOf('/') + 1)); // } } } function saveAs(Url, filename) { var blob = new Blob([''], { type: 'application/octet-stream' }); var url = URL.createObjectURL(blob); blobSaveAs(blob, filename); // reader.file = file; // reader.onerror = function(e) { // switch (e.target.error.code) { // case e.target.error.NOT_FOUND_ERR: // alert('File Not Found!'); // break; // case e.target.error.NOT_READABLE_ERR: // alert('File is not readable'); // break; // case e.target.error.ABORT_ERR: // break; // noop // default: // alert('An error occurred reading this file.'); // }; // }; // reader.onabort = function(e) { // alert('File read cancelled'); // }; // reader.onload = function(e) { // var blob = new Blob(e.target.result, { type: 'application/octet-stream' }); // //bb.append(this.result); // // } // reader.readAsBinaryString(file); //} } function blobSaveAs(blob, filename) { var type = blob.type; var force_saveable_type = 'application/octet-stream'; if (type && type != force_saveable_type) { // 强制下载,而非在浏览器中打开 var slice = blob.slice; // || blob.webkitSlice || blob.mozSlice; blob = slice.call(blob, 0, blob.size, force_saveable_type); } var url = URL.createObjectURL(blob); var save_link = document.createElementNS('xhtml', 'a'); save_link.href = url; save_link.download = filename; var event = document.createEvent('MouseEvents'); event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); save_link.dispatchEvent(event); URL.revokeObjectURL(url); } function readAsBinaryString() { var file = document.getElementById("file").files[0]; var reader = new FileReader(); //将文件以二进制形式读入页面 reader.readAsBinaryString(file); reader.onload = function(f) { var result = document.getElementById("result"); //显示文件 result.innerHTML = this.result; } }
<div><input type="file" id="file" /> <input type="button" value="读取二进制数据" onclick="readAsBinaryString()" /> <div id="result" name="result"></div></div>