UNPKG

yoyo-ng-modulewindy

Version:

服务于52ABP模板的前端开源的相关组件内容。整合了ng-alain和你NG ZORRO的内容

87 lines 3.4 kB
import { Directive, ElementRef, Input, HostListener, EventEmitter, Output, Optional, } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { saveAs } from 'file-saver'; import { _HttpClient } from 'yoyo-ng-module/theme'; /** * 文件下载 * * ```html * <button nz-button down-file http-url="assets/demo{{i}}" file-name="demo中文">{{i}}</button> * ``` */ var DownFileDirective = /** @class */ (function () { function DownFileDirective(el, http, _http) { this.el = el; this.http = http; this._http = _http; /** 请求类型 */ this.httpMethod = 'get'; /** 成功回调 */ this.success = new EventEmitter(); /** 错误回调 */ this.error = new EventEmitter(); } DownFileDirective.prototype.getDisposition = function (data) { var arr = (data || '') .split(';') .filter(function (i) { return i.includes('='); }) .map(function (v) { var strArr = v.split('='); var utfId = "UTF-8''"; var value = strArr[1]; if (value.startsWith(utfId)) value = value.substr(utfId.length); return _a = {}, _a[strArr[0].trim()] = value, _a; var _a; }); return arr.reduce(function (o, item) { return item; }, {}); }; DownFileDirective.prototype._click = function () { var _this = this; this.el.nativeElement.disabled = true; (this._http || this.http).request(this.httpMethod, this.httpUrl, { params: this.httpData || {}, responseType: 'blob', observe: 'response', }) .subscribe(function (res) { if (res.status !== 200 || res.body.size <= 0) { _this.error.emit(res); return; } var disposition = _this.getDisposition(res.headers.get('content-disposition')); var fileName = _this.fileName || disposition["filename*"] || disposition["filename"] || res.headers.get('filename') || res.headers.get('x-filename'); saveAs(res.body, decodeURI(fileName)); _this.success.emit(res); _this.el.nativeElement.disabled = false; }, function (err) { _this.error.emit(err); _this.el.nativeElement.disabled = false; }); }; DownFileDirective.decorators = [ { type: Directive, args: [{ selector: '[down-file]' },] }, ]; /** @nocollapse */ DownFileDirective.ctorParameters = function () { return [ { type: ElementRef }, { type: HttpClient }, { type: _HttpClient, decorators: [{ type: Optional }] } ]; }; DownFileDirective.propDecorators = { httpData: [{ type: Input, args: ['http-data',] }], httpMethod: [{ type: Input, args: ['http-method',] }], httpUrl: [{ type: Input, args: ['http-url',] }], fileName: [{ type: Input, args: ['file-name',] }], success: [{ type: Output }], error: [{ type: Output }], _click: [{ type: HostListener, args: ['click',] }] }; return DownFileDirective; }()); export { DownFileDirective }; //# sourceMappingURL=down-file.directive.js.map