编辑 | blame | 历史 | 原始文档
import { importFileToService, exportFileToClient } from '@/api/file'
import { ElMessage } from 'element-plus'
import { downloadFile } from '@/utils'
import dayjs from 'dayjs'

export const useFile = () => {
  /**
   * µ¼ÈëÎļþ
   * @param url
   */
  const importFile = async (url: string, file: File) => {
    const formData = new FormData()
    formData.append('file', file)
    await importFileToService(url, formData)
    ElMessage('µ¼Èë³É¹¦')
  }
  /**
   * µ¼³öÎļþ
   * @param url
   */
  const exportFile = async (url: string, params: any, name: string) => {
    const res = await exportFileToClient(url, params)
    downloadFile(res, `${name}_${dayjs().format('YYYYMMDDHHMMss')}.xlsx`)
    ElMessage.success('µ¼³ö³É¹¦')
  }

  return {
    importFile,
    exportFile,
  }
}