@ylz/plugins
Version:
ylz plugins
3 lines (2 loc) • 1.42 kB
JavaScript
/* * Copyright © 2019-2019 chenwenbin * Released under the MIT License. */
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self)["file-download"]=t()}(this,(function(){"use strict";return{downloadByData:function(e,t,o,n){var d=new Blob(void 0!==n?[n,e]:[e],{type:o||"application/octet-stream"});if(void 0!==window.navigator.msSaveBlob)window.navigator.msSaveBlob(d,t);else{var i=window.URL.createObjectURL(d),a=document.createElement("a");a.style.display="none",a.href=i,a.setAttribute("download",t),void 0===a.download&&a.setAttribute("target","_blank"),document.body.appendChild(a),a.click(),document.body.removeChild(a),window.URL.revokeObjectURL(i)}},downloadByUrl:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"_self",o=window.navigator.userAgent.toLowerCase().indexOf("chrome")>-1,n=window.navigator.userAgent.toLowerCase().indexOf("safari")>-1;if(/(iP)/g.test(window.navigator.userAgent))return console.error("您的浏览器不支持下载!"),!1;if(o||n){var d=document.createElement("a");if(d.href=e,d.target=t,void 0!==d.download){var i=e.substring(e.lastIndexOf("/")+1,e.length);d.download=i}if(document.createEvent){var a=document.createEvent("MouseEvents");return a.initEvent("click",!0,!0),d.dispatchEvent(a),!0}}return-1===e.indexOf("?")&&(e+="?download"),window.open(e,t),!0}}}));