当前位置: 首页 > 工具软件 > application > 使用案例 >

下载文档对应的application类型

周鸿运
2023-12-01
/**
 * [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;
};
 类似资料: