UNPKG

vue-simple

Version:

Use Vue in the simplest and easiest way, contain more than one of plugins and other to do that, i hope you will like it.

150 lines (128 loc) 3.35 kB
import _classCallCheck from 'babel-runtime/helpers/classCallCheck'; import _createClass from 'babel-runtime/helpers/createClass'; import MIME from './mimetype'; import { objectIndexOf } from '../utils/object'; import Downloader from './Downloader'; var Endings = { transparent: 'transparent', native: 'native' }; /** * BlobFile 文件辅助类 */ var BlobFile = function () { /** * 创建文件对象 * @param {Array<*>|Blob} data 数据内容源 * @param {String} filename 文件名(不包含扩展名) * @param {String} mime 文件 MIME 类型 * @param {String} [endings] 决定 append() 的数据格式(数据中的 \n 如何被转换)取值为 Endings.transparent 或 Endings.native */ function BlobFile(_ref) { var data = _ref.data, filename = _ref.filename, mime = _ref.mime, endings = _ref.endings; _classCallCheck(this, BlobFile); this._blob = null; this._data = null; this._filename = null; this._mime = null; this._endings = null; this._extension = null; this._data = data; this._filename = filename; this._mime = mime || MIME.bin; this._endings = endings || Endings.native; this._extension = BlobFile.parseExtension(this._mime); this.create(); } _createClass(BlobFile, [{ key: 'create', value: function create() { this._blob = new Blob([this._data], { type: this._mime, endings: this._endings }); var type = BlobFile.typeOf(this.data); switch (type) { case 'blob': { this._blob = this._data; break; } case 'arraybuffer': { this._blob = new Blob(this._data, { type: this._mime, endings: this._endings }); break; } default: { this._blob = new Blob([this._data], { type: this._mime, endings: this._endings }); } } } }, { key: 'download', /** * 立即下载该文件到本地 */ value: function download() { Downloader(this._blob, this.fullname); } /** * 注销释放内存 */ }, { key: 'destory', value: function destory() { this._blob.msClose && this._blob.msClose(); this._blob = null; this._data = null; this._filename = null; } }, { key: 'data', get: function get() { return this._data; } }, { key: 'filename', get: function get() { return this._filename; } }, { key: 'extension', get: function get() { return this._extension; } /** * 根据 mime 类型解析文件扩展名 * @param mime * @return {String} */ }, { key: 'fullname', get: function get() { return this.filename + '.' + this.extension; } }], [{ key: 'typeOf', value: function typeOf(data) { var type = null; if (data instanceof Blob) { type = 'blob'; } else if (data instanceof ArrayBuffer) { type = 'arraybuffer'; } else { type = 'text'; } return type; } }, { key: 'parseExtension', value: function parseExtension(mime) { var type = objectIndexOf(MIME, mime); return type ? type.key : 'unknow'; } }]); return BlobFile; }(); export { BlobFile, Endings };