Office对应ContentType 下载文件

Office对应ContentType

当从浏览器返回一个文件时,需要指定ContentType,以下是Office2007对应的值:

"application/vnd.openxmlformats-officedocument.wordprocessingml.template" (for .dotx files)
"application/vnd.openxmlformats-officedocument.presentationml.presentation" (for .pptx files)
"application/vnd.openxmlformats-officedocument.presentationml.slideshow" (for .ppsx files)
"application/vnd.openxmlformats-officedocument.presentationml.template" (for .potx files)
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" (for .xlsx files)
"application/vnd.openxmlformats-officedocument.spreadsheetml.template" (for .xltx files)

相对于Office2003是这样的

Response.ContentType = "application/vnd.ms-excel

// 下载excel
export function triggerDownloadExcel (res, fileName) {
//.xlsx 
 // const blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' })
  const blob = new Blob([res.data], { type: 'application/vnd.ms-excel;charset=utf-8' })

  const downloadElement = document.createElement('a')
  const href = window.URL.createObjectURL(blob)
  downloadElement.href = href
  downloadElement.download = `${fileName}`
  document.body.appendChild(downloadElement)
  downloadElement.click()
  document.body.removeChild(downloadElement) // 下载完成移除元素
  window.URL.revokeObjectURL(href) // 释放掉blob对象
}

你可能感兴趣的:(Office对应ContentType 下载文件)