angular2-download

import { Injectable } from '@angular/core';

function invorkLoadProxy(ifr, cache, callBackFn, resolve){
    let fn = function(){
        if (ifr.$$eventFn){
            ifr.removeEventListener('load', ifr.$$eventFn);
        }
        let r = cache || [];
        r.forEach((r)=>{
            document.body.removeChild(r);
        })
        callBackFn();
        resolve(true);
    }
    ifr.$$eventFn = fn;
    ifr.addEventListener('load', fn);
}

/**
* 下载服务
* @auther xiufu.wang
*/
@Injectable()
export class MbDownloadService {
   
    constructor() {}

    post(url: string, params?: {[name: string]: Array<string | number>| string | number}){
        return new Promise((resolve, reject)=>{
            let cache = [];
            params = params || {};
            let ifr = this.createHtmlDocumentAndAppendBody('iframe', {name: 'ng2downloadIframe', src: 'about:blank'}, cache);
            ifr.style.display = 'none';
            invorkLoadProxy(ifr, cache, ()=>{this.onDownLoad();}, resolve);

            let formCtrl:any = this.createHtmlDocumentAndAppendBody('form', {target: 'ng2downloadIframe', action: url, method: 'POST'}, false);
           
            for (var key in params){
                if (params[key] !== undefined && params.hasOwnProperty(key)){
                    let v = params[key];
                    if (Array.isArray(v)){
                        v = v.join(',');
                    }
                    formCtrl.appendChild(this.createHtmlDocument('input', {type: 'hidden', name: key, value: v}, false));
                }
            }
           
            formCtrl.submit();
            document.body.removeChild(formCtrl);
        });
    }

    get(url: string, params?: {[name: string]: Array<string | number>| string | number}){
        return new Promise((resolve, reject)=>{
            let cache = [];
            params = params || {};
            let ifr = this.createHtmlDocumentAndAppendBody('iframe', {name: 'ng2downloadIframe', src: 'about:blank'}, cache);
            ifr.style.display = 'none';
            invorkLoadProxy(ifr, cache, ()=>{this.onDownLoad();}, resolve);

            let urlParams = '_doc='+ new Date().getTime();
            for (let key in params){
                if (params.hasOwnProperty(key) && params[key]){
                    let v = params[key];
                    if (Array.isArray(v)){
                        v = v.join(',');
                    }
                    urlParams = urlParams + '&' + key + '=' + v;
                }
            }
           
            let p = url.indexOf('?') > 0 ? ('&' + urlParams) : ('?' + urlParams)
            ifr.setAttribute('src', url + p);
        });
     }

    private onDownLoad(){}

    private createHtmlDocument(tagName, context, cache){
        context = context || {};
        let el = document.createElement(tagName);
        for (let key in context){
            if (context.hasOwnProperty(key) && context[key]){
                el.setAttribute(key, context[key]);
            }
        }
        if (cache){
            cache.push(el);
        }
        return el;
    }

    private createHtmlDocumentAndAppendBody(tagName, context, cache){
        let el = this.createHtmlDocument(tagName, context, cache);
        document.body.appendChild(el);
        return el;
    }

    private bindEventListener(el, type, func){
        el.removeEventListener(type, func);
        el.addEventListener(type, func);
    }
}

猜你喜欢

转载自wangxiufu1985.iteye.com/blog/2343045
今日推荐