其他分享
首页 > 其他分享> > Excel打开csv乱码问题

Excel打开csv乱码问题

作者:互联网

 

import aixos from 'axios';

// https://www.jianshu.com/p/c330a911d6f5这种情况下 需要用这种办法调用下载,否则office打不开

/**
 * 从请求头获取文件名
 */
const getFileName = (contentDisposition: string): any => {
  const contentDispositionObj = {};
  const contentDispositionFmt = /\;/.test(contentDisposition) ? contentDisposition.split(';') : [contentDisposition];
  for (const item of contentDispositionFmt) {
    if (/\=/.test(item)) {
      const itemSplit = item.split('=');
      contentDispositionObj[itemSplit[0]] = itemSplit[1];
    } else {
      contentDispositionObj[item] = null;
    }
  }
  return contentDispositionObj;
};

/**
 * 下载
 */
const download = async (url) => {
  const res = await aixos({url, responseType: 'blob'});
  const fileName = getFileName(res.headers['content-disposition']).filename;
  const blobUrl = window.URL.createObjectURL(res.data);
  downloadDom(blobUrl, decodeURIComponent(fileName));
};

/**
 * 下载时创建的dom
 */
const downloadDom = (blobUrl, fileName) => {
  const a = document.createElement('a');
  a.style.display = 'none';
  a.download = fileName;
  a.href = blobUrl;
  a.click();
  document.body.removeChild(a);
};

export default download;

 

const url = window.location.origin + `/v6/api/satisfied/exportSatisfiedDetail?${query}`;
downloadHelp(url);

 

标签:const,url,blobUrl,Excel,fileName,乱码,item,contentDispositionObj,csv
来源: https://www.cnblogs.com/dshvv/p/13036641.html