下载文档对应的application类型

该代码示例展示了如何根据文件类型获取相应的MIME类型,用于处理不同类型的文件下载。它使用了一个包含多种文件类型及其对应MIME类型的数组,并通过findArrayReturnValue函数查找指定类型的MIME。然后,借助axios库下载文件,设置响应类型为blob,并创建一个隐藏的a标签实现文件的下载。
摘要由CSDN通过智能技术生成
/**
 * [getFileType 下载文档对应的类型]
 * @param  {[type]} type [description]
 * @param  {[type]} id   [description]
 * @return {[type]}      [description]
 */
export function getFileType(id) {
  let arr = [
    { type: "doc", application: "application/msword" },
    {
      type: "docx",
      application:
        "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    },
    { type: "dot", application: "application/msword" },
    {
      type: "dotx",
      application:
        "application/vnd.openxmlformats-officedocument.wordprocessingml.template"
    },
    { type: "xls", application: "application/vnd.ms-excel" },
    {
      type: "xlsx",
      application:
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    },
    { type: "ppt", application: "application/vnd.ms-powerpoint" },
    {
      type: "pptx",
      application:
        "application/vnd.openxmlformats-officedocument.presentationml.presentation"
    },
    { type: "pdf", application: "application/pdf" },
    { type: "txt", application: "text/plain" },
    { type: "gif", application: "image/gif" },
    { type: "jpeg", application: "image/jpeg" },
    { type: "jpg", application: "image/jpeg" },
    { type: "png", application: "image/png" },
    { type: "css", application: "text/css" },
    { type: "html", application: "text/html" },
    { type: "htm", application: "text/html" },
    { type: "xsl", application: "text/xml" },
    { type: "xml", application: "text/xml" },
    { type: "mpeg", application: "video/mpeg" },
    { type: "mpg", application: "video/mpeg" },
    { type: "avi", application: "video/x-msvideo" },
    { type: "movie", application: "video/x-sgi-movie" },
    { type: "bin", application: "application/octet-stream" },
    { type: "exe", application: "application/octet-stream" },
    { type: "so", application: "application/octet-stream" },
    { type: "dll", application: "application/octet-stream" },
    { type: "ai", application: "application/postscript" },
    { type: "dir", application: "application/x-director" },
    { type: "js", application: "application/x-javascript" },
    { type: "swf", application: "application/x-shockwave-flash" },
    { type: "xhtml", application: "application/xhtml+xml" },
    { type: "xht", application: "application/xhtml+xml" },
    { type: "zip", application: "application/zip" },
    { type: "mid", application: "audio/midi" },
    { type: "midi", application: "audio/midi" },
    { type: "mp3", application: "audio/mpeg" },
    { type: "rm", application: "audio/x-pn-realaudio" },
    { type: "rpm", application: "audio/x-pn-realaudio-plugin" },
    { type: "wav", application: "audio/x-wav" },
    { type: "bmp", application: "image/bmp" }
  ];
  return findArrayReturnValue(arr, "type", id, "application");
}


通过文件流下载各种类型的代码

/** download All*/
const downMethod = data => {
  axios({
    method: data.method,
    url: apiUrl + data.url,
    params: data.params,
    responseType: "blob"
  }).then(res => {
    let timestamp = new Date().getTime();
    const link = document.createElement("a");
    let blob = new Blob([res.data], {
      type: getFileType(data.params.fileType)
    });
    link.style.display = "none";
    link.href = URL.createObjectURL(blob);
    link.download = data.fileName + moment(timestamp).format("YYYY-MM-DD"); //下载的文件名
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  });
};


/**
 * @param arr 查找数组
 * @param property 查找属性
 * @param value 查找值
 * @des:查找到值后返回其值
 */
export const findArrayReturnValue = (arr, property, value, findKey) => {
  let len, findValue;
  if (!Array.isArray(arr)) {
    return;
  }
  len = arr.length;
  for (var i = 0; i < len; i++) {
    if (
      arr[i].hasOwnProperty(property) &&
      arr[i][property] == value &&
      arr[i].hasOwnProperty(findKey)
    ) {
      findValue = arr[i][findKey];
      break;
    }
  }
  return findValue;
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值