下载模版的具体代码

export function downloadFile(url, data = {}, methodType = 'GET') {
  const form = document.createElement('form');
  const formAttrs = [
    {
      name: 'action',
      value: url,
    },
    { name: 'method', value: methodType },
  ];
  formAttrs.forEach(attr => {
    const { name, value } = attr;
    form.setAttribute(name, value);
  });
  const inputs = [];
  const allDataKeys = Object.keys(data);
  allDataKeys.forEach(key => {
    inputs.push({ name: key, value: data[key] });
  });
  inputs.push(
    { name: 'signature-sessionId', value: window.name },
    { name: 'language', value: getLocale() }
  );
  inputs.forEach(input => {
    const inputElem = document.createElement('input');
    inputElem.setAttribute('type', 'hidden');
    const keys = Object.keys(input);
    keys.forEach(key => {
      inputElem.setAttribute(key, input[key]);
    });
    form.appendChild(inputElem);
  });
  document.body.appendChild(form);
  form.submit();
  document.body.removeChild(form);
}

  

posted @ 2020-09-01 14:45  lipu1993  阅读(112)  评论(0编辑  收藏  举报