shinfileinputer
Version:
select and access a local file without upload file or files to remote
1 lines • 3.98 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.shinfileinputer=e():t.shinfileinputer=e()}(window,(function(){return function(t){var e={};function i(n){if(e[n])return e[n].exports;var r=e[n]={i:n,l:!1,exports:{}};return t[n].call(r.exports,r,r.exports,i),r.l=!0,r.exports}return i.m=t,i.c=e,i.d=function(t,e,n){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(i.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var r in t)i.d(n,r,function(e){return t[e]}.bind(null,r));return n},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=0)}([function(t,e,i){"use strict";var n;i.r(e),i.d(e,"OUTOUTTYPE",(function(){return n})),function(t){t.BASE64="base64",t.TEXT="text",t.BUFFER="arraybuffer"}(n||(n={}));var r=function(){function t(t){void 0===t&&(t=!1),this._ismultiple=!1,this._limitSize=512e4,this._ismultiple=t,this.accept=".*",this.initInput(),window.hasOwnProperty("File")&&window.hasOwnProperty("FileReader")&&window.hasOwnProperty("FileList")&&window.hasOwnProperty("Blob")?console.log("Great success! All the File APIs are supported."):(console.log("The File APIs are not fully supported in this browser."),alert("你使用的浏览器不支持文件本地上传。请更新你的浏览器"))}return t.prototype.initInput=function(){var t=this;this.input=null,this.input=document.createElement("input"),this.input.type="file",this.input.accept=this.accept,this.ismultiple&&this.input.setAttribute("multiple","multiple"),this.input.onchange=function(e){t.fileSelect(e)}},t.prototype.setLimitSize=function(t){return this._limitSize=t,this},Object.defineProperty(t.prototype,"ismultiple",{get:function(){return this._ismultiple},set:function(t){this._ismultiple=t,t&&this.input?this.input.setAttribute("multiple","multiple"):!t&&this.input&&this.input.removeAttribute("multiple")},enumerable:!0,configurable:!0}),t.prototype.setMultiple=function(t){return this.ismultiple=t,this},Object.defineProperty(t.prototype,"files",{get:function(){return this._files},set:function(t){console.warn("this params is not allowed be setted")},enumerable:!0,configurable:!0}),t.prototype.fileSelect=function(t){var e=this,i=this.input.files;this._files=this.input.files;for(var n=new Set(this.outputoption),r=[],o=0;o<i.length;o++){var u=i[o];if(!u)return void console.log("没有选择文件");r.push(this.startReadFile(u,n))}Promise.all(r).then((function(t){e.callback&&e.callback.call(null,t),e.initInput()}))},t.prototype.startReadFile=function(t,e){if(t.size>this._limitSize)return Promise.resolve({data:null,msg:"size outof limited"});return new Promise((function(i,r){var o=[];e.forEach((function(e){o.push(function(e){return new Promise((function(i,r){var o=new FileReader;switch(o.onload=function(t){i({data:o.result,type:e})},e){case n.BASE64:o.readAsDataURL(t);break;case n.BUFFER:o.readAsArrayBuffer(t);break;case n.TEXT:o.readAsText(t);break;default:i({data:null,type:e,msg:"outputtype not exists"})}}))}(e))})),Promise.all(o).then((function(e){i({file:t,datas:e})}))}))},t.prototype.loadFile=function(t,e,i){if(this.callback=e,this.errorhandle=i,t&&(this.input.accept=t),document.all)this.input.click();else{var n=document.createEvent("MouseEvents");n.initEvent("click",!0,!0),this.input.dispatchEvent(n)}},t.prototype.start=function(t,e){var i=this;return this.outputoption=e||[n.BASE64],new Promise((function(e,n){i.loadFile(t,e,n)}))},t}();e.default=r}])}));