1、文件下载downloadFile函数封装

2018-11-21  本文已影响0人  快点赞呀

// 文件下载

export function downloadFile (url, data, method) {

  let t = document.getElementById('ihr_table_export_form');

  let curMethod = method || 'GET';

  if (!t) {

    t = document.createElement('form');

    t.setAttribute('id', 'ihr_table_export_form');

    t.setAttribute('style', 'display: none');

    t.setAttribute('action', process.env.API_URL + url);

    t.setAttribute('method', curMethod);

    document.body.appendChild(t);

  } else {

    t.setAttribute('action', process.env.API_URL + url);

    t.setAttribute('method', curMethod);

  }

  t.innerHTML = '';

  var input = document.createElement('input');

  input.setAttribute('name', 'lang');

  input.setAttribute('value', getStore('lang') || 'en-US');

  t.appendChild(input);

  if (data) {

    for (let key of Object.keys(data)) {

      if (data[key] !== undefined && data[key] !== null) {

        input = document.createElement('input');

        input.setAttribute('name', key);

        input.setAttribute('value', data[key]);

        t.appendChild(input);

      }

    }

  }

  t.submit();

  console.log('11111');

}

引入再调用import { downloadFile } from '@/utils/assist';

上一篇 下一篇

猜你喜欢

热点阅读